Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge branch '2016-06' of github.com:swcarpentry/styles into gh-pages
  • Loading branch information
Greg Wilson committed Jul 2, 2016
2 parents 21a3d9a + 570451a commit 8ca4b81
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
12 changes: 12 additions & 0 deletions bin/chunk-options.R
Expand Up @@ -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
Expand Down
45 changes: 43 additions & 2 deletions bin/lesson_check.py
Expand Up @@ -40,6 +40,12 @@ REQUIRED_FILES = {
# 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+$')

# Pattern to match figure references in HTML.
P_FIGURE_REFS = re.compile(r'<img[^>]+src="([^"]+)"[^>]*>')

# What kinds of blockquotes are allowed?
KNOWN_BLOCKQUOTES = {
'callout',
Expand Down Expand Up @@ -97,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()


Expand All @@ -106,6 +113,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',
Expand All @@ -118,6 +126,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')

Expand Down Expand Up @@ -192,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."""

Expand Down Expand Up @@ -263,11 +304,11 @@ class CheckBase(object):
"""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):
Expand Down

0 comments on commit 8ca4b81

Please sign in to comment.