def __init__(self, filename=None, markdown=None): """Perform validation on a Markdown document. Validator accepts either the path to a file containing Markdown, OR a valid Markdown string. The latter is useful for unit testing.""" self.filename = filename if filename: # Expect Markdown files to be in same directory as the input file self.markdown_dir = os.path.dirname(filename) self.lesson_dir = self.markdown_dir with open(filename, 'rU') as f: self.markdown = f.read() else: # Look for linked content in ../pages (relative to this file) self.lesson_dir = os.path.abspath( os.path.join(os.path.dirname(__file__), os.pardir)) self.markdown_dir = self.lesson_dir self.markdown = markdown ast = self._parse_markdown(self.markdown) self.ast = vh.CommonMarkHelper(ast) # Keep track of how many times callout box styles are used self._callout_counts = collections.Counter()
def _validate_one_html_link(self, link_node, check_text=False): """ Any local html file being linked was generated as part of the lesson. Therefore, file links (.html) must have a Markdown file in the expected folder. The title of the linked Markdown document should match the link text. """ dest, link_text = self.ast.get_link_info(link_node) # HTML files in same folder are made from Markdown; special tests fn = dest.split("#")[0] # Split anchor name from filename expected_md_fn = os.path.splitext(fn)[0] + os.extsep + "md" expected_md_path = os.path.join(self.markdown_dir, expected_md_fn) if not os.path.isfile(expected_md_path): logging.error( "In {0}: " "The document links to {1}, but could not find " "the expected markdown file {2}".format( self.filename, fn, expected_md_path)) return False if check_text is True: # If file exists, parse and validate link text = node title with open(expected_md_path, 'rU') as link_dest_file: dest_contents = link_dest_file.read() dest_ast = self._parse_markdown(dest_contents) dest_ast = vh.CommonMarkHelper(dest_ast) dest_page_title = dest_ast.get_doc_header_subtitle() if dest_page_title != link_text: logging.error( "In {0}: " "The linked page {1} exists, but " "the link text '{2}' does not match the " "(sub)title of that page, '{3}'.".format( self.filename, dest, link_text, dest_page_title)) return False return True