def generate_page(self, document, to_format): files = self.project.template_conf.metadata + document.template_conf.metadata + document.contents files = self.symbol_processor.replace_symbols_in_collection(files) files = dgen_utils.expand_paths(files) output_file = os.path.join(self.project.html_dir, document.html_filename) # Build the document contents contents = u'\n' for path in files: if os.path.isfile(path): with io.open(path, 'r', encoding='utf-8') as f: filecontents = f.read() contents = contents + filecontents + u'\n\n' else: dgen_utils.log_warn('file does not exist:', path) # Replace symbols in the content and arguments contents = self.symbol_processor.replace_symbols_in_string(contents) self.print_markdown_contents(contents) pandoc_options = self.project.template_conf.pandoc_options pandoc_options = document.template_conf.pandoc_options + pandoc_options pandoc_options = self.symbol_processor.replace_symbols_in_collection( pandoc_options) pandoc_options = [ '--output=' + dgen_utils.expand_paths(output_file), '--from=markdown', '--to=' + to_format ] + pandoc_options output = dgen_utils.run_cmd_with_io('pandoc', pandoc_options, stdindata=contents) assert output == ''
def replace_symbols_in_string(self, string, symbols=None): if symbols is None: symbols = self.symbols matches_bad = re.finditer(r'(\${.*?})', string) for bad_match in matches_bad: dgen_utils.log_warn("found wrong syntax for symbol:", bad_match.group(0)) for key, value in symbols.items(): search_string = '%{' + key + '}' string = re.sub(search_string, unicode(value), string) return string
def filter_text(text, document): text = urllib2.unquote(text) for match in PATTERN.finditer(text): field = match.group(2) result = document.get_metadata(field, None) if isinstance(result, unicode): return match.group(1) + result + match.group(3) dgen_utils.log_warn("metavar not found in document:", field) match_bad = PATTERN_BAD.match(text) if match_bad: dgen_utils.log_warn("found wrong syntax for metavar:", match_bad.group(1)) return text
def metavars(elem, document): for match in PATTERN.finditer(elem.text): field = match.group(2) result = document.get_metadata(field, None) # TODO: dead code. remove after testing #if type(result) == MetaInlines: # dgen_utils.log_warn('hitting here. shouldn\'t happen?') # result = Span(*result.content, classes=['interpolated'], attributes={'field': field}) # return Span(Str(match.group(1)), result, Str(match.group(3)), classes=['interpolated']) #el if isinstance(result, unicode): return Str(match.group(1) + result + match.group(3)) dgen_utils.log_warn("metavar not found in document:", field) match_bad = PATTERN_BAD.match(elem.text) if match_bad: dgen_utils.log_warn("found wrong syntax for metavar:", match_bad.group(1)) return None
def formatted_metavars(elem, document): classes = [] result = None # loop through all element classes and look for a matching metadata attribute for field in elem.classes: result = document.get_metadata(field) # check the match is a dictionary and then loop through the elements content looking for # a key in the dictionary (case insensitive) if isinstance(result, OrderedDict): for string in elem.content: if type(string) == Str: for key, value in result.items(): if key.lower() == string.text.lower(): classes = classes + [value] if classes != []: # update the original element with any matches elem.classes = elem.classes + classes return elem if result != None: dgen_utils.log_warn( 'match on element classes in metadata found but no matching keys\nelem: %s\nmatch:%s' % (elem, result)) return None