def serialize_reference(reference): ''' \\bibitem is what TeX inline bibliographies call each bibliographic entry returns a string ''' contents = [reference.key] + ['%s = {%s}' % (field, escape(value)) for field, value in reference.attrs.items()] body = ',\n '.join(contents) # author_re = bib_item.lastnames.join(' (?:and|&) ').gsub("\\", "\\\\\\\\") # regexes = { # citet: Regexp.new(author_re + '\s*\((\d{4}\w?,?\s*)+\)'), # posscitet: Regexp.new(author_re + '\'s \s*\((\d{4}\w?,?\s*)+\)'), # citep: Regexp.new('\(' + author_re + ',?\s*(\d{4}\w?,?\s*)+\)'), # citealt: Regexp.new(author_re + ',?\s*(\d{4}\w?,?\s*)+') # } # regexes.each do |key, regex| # @tex = @tex.gsub(regex) do |match| # refs = $1.split(',').map { |year| bib_item.ref_lastnames + ":" + year }.join(',') # "\\#{key}{#{refs}}" return u'@%s{%s\n}' % (reference.medium, body)
def serialize_document(document): ''' Converts the spans in a document into a bunch of strings yields plain bytestrings (or at least unicode that is totally ascii) ''' # counters = dict() # current_styles tracks what styles are currently applied current_styles = set() for span in document.spans: pop_styles, push_styles, current_styles = set_diff(current_styles, span.styles) # logger.debug('popping styles: %r', pop_styles) for style in pop_styles: yield '}' logger.silly('escaping: %r (pop ) (>>', span.text) # logger.debug('pushing styles: %r', push_styles) for style in push_styles: # handle the more common styles first: if style in simple_commands: yield simple_commands[style] elif style == 'hyperlink': yield r'\href{%s}' % span.attrs['url'] elif style == 'counter': yield r'\ex[]' else: raise NotImplementedError('Unrecognized style: %s (%s)', style, span) yield '{' # escape the odd characters before yielding the content, which returns ascii yield escape(span.text) else: # close by popping all styles for style in current_styles: yield '}'