Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Tighter checks for figures
  • Loading branch information
Greg Wilson committed Jul 7, 2016
1 parent e577ea8 commit 9d6512c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
8 changes: 4 additions & 4 deletions Makefile
Expand Up @@ -23,10 +23,6 @@ serve : lesson-rmd
site : lesson-rmd
${JEKYLL} build --config _config.yml,_config_dev.yml

## figures : re-generate inclusion displaying all figures.
figures :
@bin/extract_figures.py -s _episodes -p ${PARSER} > _includes/all_figures.html

# repo-check : check repository settings.
repo-check :
@bin/repo_check.py -s .
Expand Down Expand Up @@ -96,6 +92,10 @@ lesson-check :
lesson-check-all :
@bin/lesson_check.py -s . -p ${PARSER} -l -w

## lesson-figures : re-generate inclusion displaying all figures.
lesson-figures :
@bin/extract_figures.py -p ${PARSER} ${MARKDOWN_SRC} > _includes/all_figures.html

## unittest : run unit tests on checking tools.
unittest :
python bin/test_lesson_check.py
Expand Down
36 changes: 12 additions & 24 deletions bin/extract_figures.py
Expand Up @@ -5,24 +5,15 @@ import os
import glob
from optparse import OptionParser

from util import Reporter, read_markdown


# Things an image file's name can end with.
PATH_SUFFICES = {
'.gif',
'.jpg',
'.png',
'.svg'
}
from util import Reporter, read_markdown, IMAGE_FILE_SUFFIX


def main():
"""Main driver."""

args = parse_args()
images = []
for filename in get_filenames(args.source_dir):
for filename in args.filenames:
images += get_images(args.parser, filename)
save(sys.stdout, images)

Expand All @@ -35,19 +26,14 @@ def parse_args():
default=None,
dest='parser',
help='path to Markdown parser')
parser.add_option('-s', '--source',
default=None,
dest='source_dir',
help='source directory')

args, extras = parser.parse_args()
require(args.parser is not None,
'Path to Markdown parser not provided')
require(args.source_dir is not None,
'Source directory not provided')
require(not extras,
'Unexpected trailing command-line arguments "{0}"'.format(extras))
require(extras,
'No filenames specified')

args.filenames = extras
return args


Expand All @@ -70,20 +56,22 @@ def get_images(parser, filename):
def find_image_nodes(doc, result):
"""Find all nested nodes representing images."""

if (doc["type"] == "img") or \
((doc["type"] == "html_element") and (doc["value"] == "img")):
if (doc['type'] == 'img') or \
((doc['type'] == 'html_element') and (doc['value'] == 'img')):
result.append({'alt': doc['attr']['alt'], 'src': doc['attr']['src']})
else:
for child in doc.get("children", []):
for child in doc.get('children', []):
find_image_nodes(child, result)


def find_image_links(doc, result):
"""Find all links to files in the 'fig' directory."""

if (doc['type'] == 'a') and ('attr' in doc) and ('href' in doc['attr']):
if ((doc['type'] == 'a') and ('attr' in doc) and ('href' in doc['attr'])) \
or \
((doc['type'] == 'html_element') and (doc['value'] == 'a')):
path = doc['attr']['href']
if os.path.splitext(path)[1].lower() in PATH_SUFFICES:
if os.path.splitext(path)[1].lower() in IMAGE_FILE_SUFFIX:
result.append({'alt':'', 'src': doc['attr']['href']})
else:
for child in doc.get('children', []):
Expand Down
6 changes: 3 additions & 3 deletions bin/lesson_check.py
Expand Up @@ -11,7 +11,7 @@ import json
import re
from optparse import OptionParser

from util import Reporter, read_markdown, load_yaml, check_unwanted_files, require
from util import Reporter, read_markdown, load_yaml, check_unwanted_files, require, IMAGE_FILE_SUFFIX

__version__ = '0.2'

Expand Down Expand Up @@ -227,9 +227,9 @@ def check_figures(source_dir, reporter):
'File not found')
return

# Get actual files.
# Get actual image files (ignore non-image files).
fig_dir_path = os.path.join(source_dir, 'fig')
actual = [f for f in os.listdir(fig_dir_path) if not f.startswith('.')]
actual = [f for f in os.listdir(fig_dir_path) if os.path.splitext(f)[1] in IMAGE_FILE_SUFFIX]

# Report differences.
unexpected = set(actual) - set(referenced)
Expand Down
12 changes: 11 additions & 1 deletion bin/util.py
Expand Up @@ -11,11 +11,21 @@ except ImportError:
sys.exit(1)


# Things an image file's name can end with.
IMAGE_FILE_SUFFIX = {
'.gif',
'.jpg',
'.png',
'.svg'
}

# Files that shouldn't be present.
UNWANTED_FILES = [
'.nojekyll'
]


# Marker to show that an expected value hasn't been provided.
# (Can't use 'None' because that might be a legitimate value.)
REPORTER_NOT_SET = []

class Reporter(object):
Expand Down

0 comments on commit 9d6512c

Please sign in to comment.