From 47bfe1bdb97f7925ace52ed71aa93ba828c709f7 Mon Sep 17 00:00:00 2001 From: Raniere Silva Date: Sun, 5 Nov 2017 09:29:51 +0000 Subject: [PATCH] Replace extract_figures.py with Javacript Demo at https://github.com/swcarpentry/lesson-example/pull/132/. --- Makefile | 4 -- bin/extract_figures.py | 98 ---------------------------------------- bin/lesson_initialize.py | 41 ++++++++++++++--- 3 files changed, 35 insertions(+), 108 deletions(-) delete mode 100755 bin/extract_figures.py diff --git a/Makefile b/Makefile index 8dc2541..df31f00 100644 --- a/Makefile +++ b/Makefile @@ -95,10 +95,6 @@ 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 diff --git a/bin/extract_figures.py b/bin/extract_figures.py deleted file mode 100755 index 63a7752..0000000 --- a/bin/extract_figures.py +++ /dev/null @@ -1,98 +0,0 @@ -#!/usr/bin/env python - -from __future__ import print_function -import sys -import os -import glob -from optparse import OptionParser - -from util import Reporter, read_markdown, IMAGE_FILE_SUFFIX - -def main(): - """Main driver.""" - - args = parse_args() - images = [] - for filename in args.filenames: - images += get_images(args.parser, filename) - save(sys.stdout, images) - - -def parse_args(): - """Parse command-line arguments.""" - - parser = OptionParser() - parser.add_option('-p', '--parser', - default=None, - dest='parser', - help='path to Markdown parser') - - args, extras = parser.parse_args() - require(args.parser is not None, - 'Path to Markdown parser not provided') - require(extras, - 'No filenames specified') - - args.filenames = extras - return args - - -def get_filenames(source_dir): - """Get all filenames to be searched for images.""" - - return glob.glob(os.path.join(source_dir, '*.md')) - - -def get_images(parser, filename): - """Extract all images from file.""" - - content = read_markdown(parser, filename) - result = [] - find_image_nodes(content['doc'], result) - find_image_links(content['doc'], result) - return result - - -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')): - alt = doc['attr'].get('alt', '') - result.append({'alt': alt, 'src': doc['attr']['src']}) - else: - 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'])) \ - or \ - ((doc['type'] == 'html_element') and (doc['value'] == 'a') and ('href' in doc['attr'])): - path = doc['attr']['href'] - 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', []): - find_image_links(child, result) - - -def save(stream, images): - """Save results as Markdown.""" - - text = '\n
\n'.join(['

{0}

'.format(img['alt'], img['src']) for img in images]) - print(text, file=stream) - - -def require(condition, message): - """Fail if condition not met.""" - - if not condition: - print(message, file=sys.stderr) - sys.exit(1) - - -if __name__ == '__main__': - main() diff --git a/bin/lesson_initialize.py b/bin/lesson_initialize.py index bf0ba17..6e91826 100755 --- a/bin/lesson_initialize.py +++ b/bin/lesson_initialize.py @@ -351,7 +351,41 @@ EXTRAS_FIGURES_MD = '''\ layout: page title: Figures --- -{% include all_figures.html %} + +{% comment %} +Create anchor for each one of the episodes. +{% endcomment %} +{% for episode in site.episodes %} +
+{% endfor %} ''' EXTRAS_GUIDE_MD = '''\ @@ -362,10 +396,6 @@ title: "Instructor Notes" FIXME ''' -INCLUDES_ALL_FIGURES_HTML = '''\ - -''' - BOILERPLATE = ( ('AUTHORS', ROOT_AUTHORS), ('CITATION', ROOT_CITATION), @@ -380,7 +410,6 @@ BOILERPLATE = ( ('_extras/discuss.md', EXTRAS_DISCUSS_MD), ('_extras/figures.md', EXTRAS_FIGURES_MD), ('_extras/guide.md', EXTRAS_GUIDE_MD), - ('_includes/all_figures.html', INCLUDES_ALL_FIGURES_HTML) ) -- 2.30.2