Commit c2f89a7a authored by Greg Wilson's avatar Greg Wilson
Browse files

Checking ends of lines

parent acb49f78
Loading
Loading
Loading
Loading
+16 −11
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ HTML_DST = \
  $(patsubst _extras/%.md,${DST}/%/index.html,$(wildcard _extras/*.md)) \
  ${DST}/license/index.html

## lesson-rmd:    : convert Rmarkdown files to markdown
## lesson-rmd       : convert Rmarkdown files to markdown
lesson-rmd: $(RMD_SRC)
	@bin/knit_lessons.sh $(RMD_SRC)

@@ -87,6 +87,11 @@ lesson-rmd: $(RMD_SRC)
lesson-check :
	@bin/lesson_check.py -s . -p ${PARSER}

## lesson-check-all : validate lesson Markdown, checking line lengths and trailing whitespace.
lesson-check-all :
	@bin/lesson_check.py -s . -p ${PARSER} -l -w

## unittest         : run unit tests on checking tools.
unittest :
	python bin/test_lesson_check.py

+21 −5
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ import json
import re
from optparse import OptionParser

from util import Reporter, read_markdown
from util import Reporter, read_markdown, load_yaml

__version__ = '0.2'

@@ -106,7 +106,7 @@ def parse_args():
    parser = OptionParser()
    parser.add_option('-l', '--linelen',
                      default=False,
                      dest='line_len',
                      dest='line_lengths',
                      help='Check line lengths')
    parser.add_option('-p', '--parser',
                      default=None,
@@ -116,6 +116,10 @@ def parse_args():
                      default=os.curdir,
                      dest='source_dir',
                      help='source directory')
    parser.add_option('-w', '--whitespace',
                      default=False,
                      dest='trailing_whitespace',
                      help='Check for trailing whitespace')

    args, extras = parser.parse_args()
    require(args.parser is not None,
@@ -227,7 +231,8 @@ class CheckBase(object):
        """Run tests on metadata."""

        self.check_metadata()
        self.check_text()
        self.check_line_lengths()
        self.check_trailing_whitespace()
        self.check_blockquote_classes()
        self.check_codeblock_classes()

@@ -243,10 +248,10 @@ class CheckBase(object):
            self.reporter.check_field(self.filename, 'metadata', self.metadata, 'layout', self.layout)


    def check_text(self):
    def check_line_lengths(self):
        """Check the raw text of the lesson body."""

        if self.args.line_len:
        if self.args.line_lengths:
            over = [i for (i, l, n) in self.lines if (n > MAX_LINE_LEN) and (not l.startswith('!'))]
            self.reporter.check(not over,
                                self.filename,
@@ -254,6 +259,17 @@ class CheckBase(object):
                                ', '.join([str(i) for i in over]))


    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(' ')]
            self.reporter.check(not trailing,
                                self.filename,
                                'Line(s) end with whitespace: {0}',
                                ', '.join([str[i] for i in over]))


    def check_blockquote_classes(self):
        """Check that all blockquotes have known classes."""

+8 −4
Original line number Diff line number Diff line
@@ -114,9 +114,13 @@ def split_metadata(path, text):

def load_yaml(filename):
    """
    Wrapper around YAML loading so that 'import yaml' and error
    handling is only needed in one place.
    Wrapper around YAML loading so that 'import yaml' is only needed
    in one file.
    """

    try:
        with open(filename, 'r') as reader:
            return yaml.load(reader)
    except (yaml.YAMLError, FileNotFoundError) as e:
        print('Unable to load YAML file {0}:\n{1}'.format(filename, e), file=sys.stderr)
        sys.exit(1)