From 634e055b258edd492180fbcfd15664a3f4b7263e Mon Sep 17 00:00:00 2001 From: Francois Michonneau Date: Fri, 1 Jul 2016 14:08:58 -0400 Subject: [PATCH 1/3] add function knitr_fig_path --- bin/chunk-options.R | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/bin/chunk-options.R b/bin/chunk-options.R index 5836973..d956f60 100644 --- a/bin/chunk-options.R +++ b/bin/chunk-options.R @@ -8,6 +8,18 @@ library("knitr") fix_fig_path <- function(pth) file.path("..", pth) + +## We set the path for the figures globally below, so if we want to +## customize it for individual episodes, we can append a prefix to the +## global path. For instance, if we call knitr_fig_path("01-") in the +## first episode of the lesson, it will generate the figures in +## `fig/rmd-01-` +knitr_fig_path <- function(prefix) { + new_path <- paste0(opts_chunk$get("fig.path"), + prefix) + opts_chunk$set(fig.path = new_path) +} + ## We use the rmd- prefix for the figures generated by the lssons so ## they can be easily identified and deleted by `make clean-rmd`. The ## working directory when the lessons are generated is the root so the From fb17830636a349a73491a28b21624bf2c74a10f1 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Fri, 1 Jul 2016 14:10:07 -0400 Subject: [PATCH 2/3] Lesson checking parameter handling --- bin/lesson_check.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/bin/lesson_check.py b/bin/lesson_check.py index c85661e..aaec64f 100755 --- a/bin/lesson_check.py +++ b/bin/lesson_check.py @@ -40,6 +40,9 @@ # Episode filename pattern. P_EPISODE_FILENAME = re.compile(r'/_episodes/(\d\d)-[-\w]+.md$') +# Pattern to match lines ending with whitespace. +P_TRAILING_WHITESPACE = re.compile(r'\s+$') + # What kinds of blockquotes are allowed? KNOWN_BLOCKQUOTES = { 'callout', @@ -106,6 +109,7 @@ def parse_args(): parser = OptionParser() parser.add_option('-l', '--linelen', default=False, + action="store_true", dest='line_lengths', help='Check line lengths') parser.add_option('-p', '--parser', @@ -118,6 +122,7 @@ def parse_args(): help='source directory') parser.add_option('-w', '--whitespace', default=False, + action="store_true", dest='trailing_whitespace', help='Check for trailing whitespace') @@ -263,11 +268,11 @@ def check_trailing_whitespace(self): """Check for whitespace at the ends of lines.""" if self.args.trailing_whitespace: - trailing = [i for (i, l, n) in self.lines if l.endswidth(' ')] + trailing = [i for (i, l, n) in self.lines if P_TRAILING_WHITESPACE.match(l)] self.reporter.check(not trailing, self.filename, 'Line(s) end with whitespace: {0}', - ', '.join([str[i] for i in over])) + ', '.join([str(i) for i in trailing])) def check_blockquote_classes(self): From 030ac5c0854807db9325810cdc2caa7fc933ab24 Mon Sep 17 00:00:00 2001 From: Greg Wilson Date: Sat, 2 Jul 2016 17:30:22 -0400 Subject: [PATCH 3/3] Looking for missing or superfluous images --- bin/lesson_check.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/bin/lesson_check.py b/bin/lesson_check.py index aaec64f..6de7714 100755 --- a/bin/lesson_check.py +++ b/bin/lesson_check.py @@ -43,6 +43,9 @@ # Pattern to match lines ending with whitespace. P_TRAILING_WHITESPACE = re.compile(r'\s+$') +# Pattern to match figure references in HTML. +P_FIGURE_REFS = re.compile(r']+src="([^"]+)"[^>]*>') + # What kinds of blockquotes are allowed? KNOWN_BLOCKQUOTES = { 'callout', @@ -100,6 +103,7 @@ def main(): for filename in docs.keys(): checker = create_checker(args, filename, docs[filename]) checker.check() + check_figures(args.source_dir, args.reporter) args.reporter.report() @@ -197,6 +201,38 @@ def check_fileset(source_dir, reporter, filenames_present): seen) +def check_figures(source_dir, reporter): + """Check that all figures are present and referenced.""" + + # Get references. + try: + all_figures_html = os.path.join(source_dir, '_includes', 'all_figures.html') + with open(all_figures_html, 'r') as reader: + text = reader.read() + figures = P_FIGURE_REFS.findall(text) + referenced = [os.path.split(f)[1] for f in figures if '/fig/' in f] + except FileNotFoundError as e: + reporter.add(all_figures_html, + 'File not found') + return + + # Get actual files. + fig_dir_path = os.path.join(source_dir, 'fig') + actual = [f for f in os.listdir(fig_dir_path) if not f.startswith('.')] + + # Report differences. + unexpected = set(actual) - set(referenced) + reporter.check(not unexpected, + None, + 'Unexpected image files: {0}', + ', '.join(sorted(unexpected))) + missing = set(referenced) - set(actual) + reporter.check(not missing, + None, + 'Missing image files: {0}', + ', '.join(sorted(missing))) + + def create_checker(args, filename, info): """Create appropriate checker for file."""