Exemple #1
0
def validate_markdown(args):
    """
  Ensure the yaml encoded inside a YAML file is syntactically valid.

  First attempt to strip the yaml from the .md file, second use the standard python yaml parser
  to parse the embedded yaml. If it can't be passed then an error will be thrown and a stack
  trace shown. In future we may try and catch this error and provide a user-friendly report).

  In future we also perform additional structural validation on the yaml - check certain fields
  are present etc. This could be done in various ways, e.g. jsonschema, programmatic checks. We
  should also check translation -> jsonld -> rdf works as expected.
  """
    def validate_structure(obj):
        """
    Given an object, check to see if it has 'id', 'title', and 'layout' fields. If any are
    missing, collect them in a list of errors which is then returned.
    """
        errs = []
        id = obj.get('id') or ''
        if not id:
            errs.append("No id: ")
        if 'title' not in obj:
            errs.append("No title: " + id)
        if 'layout' not in obj:
            errs.append("No layout tag: " + id +
                        " -- this is required for proper rendering")
        # is_obsolete = ('is_obsolete' in obj)
        # if 'description' not in obj:
        #   errs.append("No description: " + id + " " + ("OBS" if is_obsolete else ""))
        return errs

    errs = []
    warn = []
    for fn in args.files:
        # we don't do anything with the results; an
        # error is thrown
        if not frontmatter.check(fn):
            errs.append("%s does not contain frontmatter" % (fn))
        yamltext = get_YAML_text(fn)
        yaml_config = config.YamlLintConfig(file="util/config.yamllint")
        for p in linter.run("---\n" + yamltext, yaml_config):
            if p.level == "error":
                errs.append(f"%s: {p}" % (fn))
            elif p.level == "warning":
                warn.append(f"%s: {p}" % (fn))
        (obj, md) = load_md(fn)
        errs += validate_structure(obj)
    if len(warn) > 0:
        print("WARNINGS:", file=sys.stderr)
        for w in warn:
            print("WARN: " + w, file=sys.stderr)
    if len(errs) > 0:
        print("FAILURES:", file=sys.stderr)
        for e in errs:
            print("ERROR:" + e, file=sys.stderr)
        sys.exit(1)
Exemple #2
0
    def test_check_empty_frontmatter(self):
        "Checks if a file has a frontmatter (empty or not)"
        ret = frontmatter.check("tests/empty-frontmatter.txt")

        self.assertEqual(ret, True)
Exemple #3
0
    def test_check_no_frontmatter(self):
        "Checks if a file does not have a frontmatter"
        ret = frontmatter.check("tests/no-frontmatter.txt")

        self.assertEqual(ret, False)