예제 #1
0
파일: __init__.py 프로젝트: skbly7/redmine
def format_database(
    from_filename, to_filename,
    bib_format=None, output_backend=None,
    input_encoding=None, output_encoding=None,
    parser_options=None,
    min_crossrefs=2,
    style=None,
    **kwargs
):
    if parser_options is None:
        parser_options = {}
    output_backend = find_plugin('pybtex.backends', output_backend, filename=to_filename)

    bib_data = database.parse_file(
        from_filename,
        encoding=input_encoding, bib_format=bib_format,
        **parser_options
    )
    style_cls = find_plugin('pybtex.style.formatting', style)
    style = style_cls(
        label_style=kwargs.get('label_style'),
        name_style=kwargs.get('name_style'),
        sorting_style=kwargs.get('sorting_style'),
        abbreviate_names=kwargs.get('abbreviate_names'),
        min_crossrefs=min_crossrefs,
    )
    formatted_bibliography = style.format_bibliography(bib_data)
    output_backend(output_encoding).write_to_file(formatted_bibliography, to_filename)
예제 #2
0
def references_to_markdown(references):
    """Utility function to convert a BibTeX string containing
    references into a Markdown string.

    Args:
      references: BibTeX string

    Returns:
      Markdown string

    """

    pybtex_style = find_plugin('pybtex.style.formatting', 'plain')()
    pybtex_md_backend = find_plugin('pybtex.backends', 'markdown')
    pybtex_parser = Parser()

    # hack to not print labels (may remove this later)
    def write_entry(self, key, label, text):
        self.output(u'%s  \n' % text)

    pybtex_md_backend.write_entry = write_entry
    pybtex_md_backend = pybtex_md_backend()

    data = pybtex_parser.parse_stream(StringIO(references))
    data_formatted = pybtex_style.format_entries(data.entries.itervalues())
    output = StringIO()
    pybtex_md_backend.write_to_stream(data_formatted, output)

    # add blockquote style
    references_md = '> {}'.format(output.getvalue())
    references_md.replace('\n', '\n> ')

    return references_md
예제 #3
0
 def apply(self):
     """Transform each
     :class:`~sphinxcontrib.bibtex2.foot_nodes.bibliography` node into a
     list of citations.
     """
     env = self.document.settings.env
     for bibnode in self.document.traverse(bibliography):
         id_ = bibnode['ids'][0]
         entries = [
             get_bibliography_entry(env.bibtex_bibfiles, key)
             for key in env.footbib_cache.cited[env.docname][id_]
         ]
         entries2 = [entry for entry in entries if entry is not None]
         # locate and instantiate style and backend plugins
         style = find_plugin('pybtex.style.formatting',
                             env.app.config.bibtex_style)()
         backend = find_plugin('pybtex.backends', 'docutils')()
         # create footnote nodes for all references
         footnotes = docutils.nodes.paragraph()
         # remind: style.format_entries modifies entries in unpickable way
         for entry in style.format_entries(entries2):
             footnote = backend.footnote(entry, self.document)
             node_text_transform(footnote, transform_url_command)
             footnotes += footnote
         if env.bibtex_footbibliography_header is not None:
             nodes = [
                 env.bibtex_footbibliography_header.deepcopy(), footnotes
             ]
         else:
             nodes = footnotes
         bibnode.replace_self(nodes)
예제 #4
0
    def format_from_files(self,
                          bib_files_or_filenames,
                          style,
                          citations=['*'],
                          bib_format=None,
                          bib_encoding=None,
                          output_backend=None,
                          output_encoding=None,
                          min_crossrefs=2,
                          output_filename=None,
                          add_output_suffix=False,
                          **kwargs):
        """
        Read the bigliography data from the given files and produce a formated
        bibliography.

        :param bib_files_or_filenames: A list of file names or file objects.
        :param style: The name of the formatting style.
        :param citations: A list of citation keys.
        :param bib_format: The name of the bibliography format. The default
            format is ``bibtex``.
        :param bib_encoding: Encoding of bibliography files.
        :param output_backend: Which output backend to use. The default is ``latex``.
        :param output_encoding: Encoding that will be used by the output backend.
        :param bst_encoding: Encoding of the ``.bst`` file.
        :param min_crossrefs: Include cross-referenced entries after this many
            crossrefs. See BibTeX manual for details.
        :param output_filename: If ``None``, the result will be returned as a
            string. Else, the result will be written to the specified file.
        :param add_output_suffix: Append default suffix to the output file
            name (``.bbl`` for LaTeX, ``.html`` for HTML, etc.).
        """

        from pybtex.plugin import find_plugin

        bib_parser = find_plugin('pybtex.database.input', bib_format)
        bib_data = bib_parser(
            encoding=bib_encoding,
            wanted_entries=citations,
            min_crossrefs=min_crossrefs,
        ).parse_files(bib_files_or_filenames)

        style_cls = find_plugin('pybtex.style.formatting', style)
        style = style_cls(
            label_style=kwargs.get('label_style'),
            name_style=kwargs.get('name_style'),
            sorting_style=kwargs.get('sorting_style'),
            abbreviate_names=kwargs.get('abbreviate_names'),
            min_crossrefs=min_crossrefs,
        )
        formatted_bibliography = style.format_bibliography(bib_data, citations)

        output_backend = find_plugin('pybtex.backends', output_backend)
        if add_output_suffix:
            output_filename = output_filename + output_backend.default_suffix
        if not output_filename:
            import io
            output_filename = io.StringIO()
        return output_backend(output_encoding).write_to_file(
            formatted_bibliography, output_filename)
예제 #5
0
    def __init__(self, min_crossrefs=2, **kwargs):
        self.format_name = find_plugin('pybtex.style.names', 'plain')().format
        self.format_first_name = find_plugin('pybtex.style.names', 'lastfirst')().format
        self.format_labels = find_plugin('pybtex.style.labels', None)().format_labels

        self.sort = find_plugin('pybtex.style.sorting', None)().sort
        self.min_crossrefs = min_crossrefs
예제 #6
0
    def createHTML(self, parent, token, page):

        try:
            style = find_plugin('pybtex.style.formatting', token['bib_style'])
        except PluginNotFound:
            msg = 'Unknown bibliography style "{}".'
            raise exceptions.MooseDocsException(msg, token['bib_style'])

        citations = list()
        for tok in anytree.PreOrderIter(token.root):
            if tok.name == 'BibtexCite':
                citations.extend(tok['keys'])

        formatted_bibliography = style().format_bibliography(
            self.extension.database, citations)
        html_backend = find_plugin('pybtex.backends', 'html')

        ol = html.Tag(parent, 'ol', class_='moose-bibliogrpahy')

        backend = html_backend(encoding='utf-8')
        for entry in formatted_bibliography:
            text = entry.text.render(backend)
            html.Tag(ol, 'li', id_=entry.key, string=text)

        return ol
예제 #7
0
    def createHTML(self, token, parent):

        try:
            style = find_plugin('pybtex.style.formatting', token.style)
        except PluginNotFound:
            msg = 'Unknown bibliography style "{}".'
            raise exceptions.RenderException(msg, token.style)

        citations = set()
        for tok in anytree.PreOrderIter(token.root):
            if isinstance(tok, BibtexCite):
                citations.update(tok.keys)

        formatted_bibliography = style().format_bibliography(
            self.extension.database, citations)
        html_backend = find_plugin('pybtex.backends', 'html')

        ol = html.Tag(parent, 'ol', class_='moose-bibliogrpahy')

        backend = html_backend(encoding='utf-8')
        for entry in formatted_bibliography:
            text = entry.text.render(backend)
            html.Tag(ol, 'li', id_=entry.key, string=text)

        return ol
예제 #8
0
  def run(self, lines):
    """
    Create a bibliography from cite commands.
    """

    # Join the content to enable regex searches throughout entire text
    content = '\n'.join(lines)

    # Build the database of bibtex data
    bibfiles = []
    match = re.search(self.RE_BIBLIOGRAPHY, content)
    if match:
      bib_string = match.group(0)
      for bfile in match.group(1).split(','):
        try:
          bibfiles.append(os.path.join(self._root, bfile))
          data = parse_file(bibfiles[-1])
        except:
          log.error('Failed to parse bibtex file: {}'.format(bfile))
          return lines
        self._bibtex.add_entries(data.entries.iteritems())
    else:
      return lines

    # Determine the style
    match = re.search(self.RE_STYLE, content)
    if match:
      content = content.replace(match.group(0), '')
      try:
        style = find_plugin('pybtex.style.formatting', match.group(1))
      except:
        log.error('Unknown bibliography style "{}"'.format(match.group(1)))
        return lines

    else:
      style = find_plugin('pybtex.style.formatting', 'plain')

    # Replace citations with author date, as an anchor
    content = re.sub(self.RE_CITE, self.authors, content)

    # Create html bibliography
    if self._citations:

      # Generate formatted html using pybtex
      formatted_bibliography = style().format_bibliography(self._bibtex, self._citations)
      backend = find_plugin('pybtex.backends', 'html')
      stream = io.StringIO()
      backend().write_to_stream(formatted_bibliography, stream)

      # Strip the bib items from the formatted html
      html = re.findall(r'\<dd\>(.*?)\</dd\>', stream.getvalue(), flags=re.MULTILINE|re.DOTALL)

      # Produces an ordered list with anchors to the citations
      output = u'<ol class="moose-bibliography" data-moose-bibfiles="{}">\n'.format(str(bibfiles))
      for i, item in enumerate(html):
        output += u'<li name="{}">{}</li>\n'.format(self._citations[i], item)
      output += u'</ol>\n'
      content = re.sub(self.RE_BIBLIOGRAPHY, output, content)

    return content.split('\n')
예제 #9
0
def convert(from_filename,
            to_filename,
            from_format=None,
            to_format=None,
            input_encoding=None,
            output_encoding=None,
            parser_options=None,
            preserve_case=True,
            **kwargs):
    if parser_options is None:
        parser_options = {}
    input_format = find_plugin('pybtex.database.input',
                               name=from_format,
                               filename=from_filename)
    output_format = find_plugin('pybtex.database.output',
                                name=to_format,
                                filename=to_filename)

    if from_filename == to_filename:
        raise ConvertError('input and output file can not be the same')

    bib_data = input_format(input_encoding,
                            **parser_options).parse_file(from_filename)
    if not preserve_case:
        bib_data = bib_data.lower()
    output_format(output_encoding).write_file(bib_data, to_filename)
예제 #10
0
    def createHTML(self, parent, token, page):

        try:
            style = find_plugin('pybtex.style.formatting', token['bib_style'])
        except PluginNotFound:
            msg = 'Unknown bibliography style "{}".'
            raise exceptions.MooseDocsException(msg, token['bib_style'])

        citations = self.getCitations(parent, token, page)
        formatted_bibliography = style().format_bibliography(
            self.extension.database(), citations)

        if formatted_bibliography.entries:
            html_backend = find_plugin('pybtex.backends', 'html')
            div = html.Tag(parent, 'div', class_='moose-bibliography')
            ol = html.Tag(div, 'ol')

            backend = html_backend(encoding='utf-8')
            for entry in formatted_bibliography:
                text = entry.text.render(backend)
                html.Tag(ol, 'li', id_=entry.key, string=text)

            return ol

        else:
            html.String(parent,
                        content="No citations exist within this document.")
예제 #11
0
파일: __init__.py 프로젝트: rybesh/pybtex
 def __init__(self, label_style=None, name_style=None, abbreviate_names=False, **kwargs):
     if name_style is None:
         name_style = find_plugin('pybtex.style.names')
     if label_style is None:
         label_style = find_plugin('pybtex.style.labels')
     self.format_label = label_style().format
     self.format_name = name_style().format
     self.abbreviate_names = abbreviate_names
예제 #12
0
 def __init__(self, label_style=None, name_style=None, sorting_style=None, abbreviate_names=False, **kwargs):
     self.name_style = find_plugin('pybtex.style.names', name_style or self.default_name_style)()
     self.label_style = LabelStyle()
     self.sorting_style = find_plugin('pybtex.style.sorting', sorting_style or self.default_sorting_style)()
     self.format_name = self.name_style.format
     self.format_labels = self.label_style.format_labels
     self.sort = self.sorting_style.sort
     self.abbreviate_names = abbreviate_names
예제 #13
0
	def export(self, backend):
		output_backend = find_plugin('pybtex.backends', backend)
		style_cls = find_plugin('pybtex.style.formatting', 'plain')
		style = style_cls()
		formatted_entries = style.format_entries([self.to_pybtex()])
		# formatted_bibliography = FormattedBibliography([e for e in formatted_entries], style)
		ob = output_backend(None)
		return formatted_entries.next().text.render(ob)
예제 #14
0
 def __init__(self, label_style=None, name_style=None, sorting_style=None, abbreviate_names=False, **kwargs):
     self.name_style = find_plugin('pybtex.style.names', name_style or self.default_name_style)()
     self.label_style = find_plugin('pybtex.style.labels', label_style or self.default_label_style)()
     self.sorting_style = find_plugin('pybtex.style.sorting', sorting_style or self.default_sorting_style)()
     self.format_name = self.name_style.format
     self.format_label = self.label_style.format
     self.sort = self.sorting_style.sort
     self.abbreviate_names = abbreviate_names
예제 #15
0
    def __init__(self, bib_format):
        super(PybtexDatabaseIO, self).__init__()
        self.bib_format = bib_format
        self.writer = find_plugin('pybtex.database.output', bib_format)(encoding='UTF-8')
        self.parser = find_plugin('pybtex.database.input', bib_format)(encoding='UTF-8')

        if bib_format == 'bibtexml':
            # BibTeXML does not support TeX preambles
            self.reference_data._preamble = []
예제 #16
0
 def apply(self):
     """Transform each
     :class:`~sphinxcontrib.bibtex.nodes.bibliography` node into a
     list of citations.
     """
     env = self.document.settings.env
     docname = env.docname
     for bibnode in self.document.traverse(bibliography):
         id_ = bibnode['ids'][0]
         bibcache = env.bibtex_cache.get_bibliography_cache(
             docname=docname, id_=id_)
         entries = env.bibtex_cache.get_bibliography_entries(
             docname=docname, id_=id_, warn=env.app.warn)
         # locate and instantiate style and backend plugins
         style = find_plugin('pybtex.style.formatting', bibcache.style)()
         backend = find_plugin('pybtex.backends', 'docutils')()
         # create citation nodes for all references
         if bibcache.list_ == "enumerated":
             nodes = docutils.nodes.enumerated_list()
             nodes['enumtype'] = bibcache.enumtype
             if bibcache.start >= 1:
                 nodes['start'] = bibcache.start
                 env.bibtex_cache.set_enum_count(
                     env.docname, bibcache.start)
             else:
                 nodes['start'] = env.bibtex_cache.get_enum_count(
                     env.docname)
         elif bibcache.list_ == "bullet":
             nodes = docutils.nodes.bullet_list()
         else:  # "citation"
             nodes = docutils.nodes.paragraph()
         # remind: style.format_entries modifies entries in unpickable way
         for entry in style.format_entries(entries):
             if bibcache.list_ == "enumerated" or bibcache.list_ == "bullet":
                 citation = docutils.nodes.list_item()
                 citation += backend.paragraph(entry)
             else:  # "citation"
                 citation = backend.citation(entry, self.document)
                 # backend.citation(...) uses entry.key as citation label
                 # we change it to entry.label later onwards
                 # but we must note the entry.label now;
                 # at this point, we also already prefix the label
                 key = citation[0].astext()
                 bibcache.labels[key] = bibcache.labelprefix + entry.label
             node_text_transform(citation, transform_url_command)
             if bibcache.curly_bracket_strip:
                 node_text_transform(
                     citation,
                     transform_curly_bracket_strip)
             nodes += citation
             if bibcache.list_ == "enumerated":
                 env.bibtex_cache.inc_enum_count(env.docname)
         bibnode.replace_self(nodes)
예제 #17
0
 def apply(self):
     """Transform each
     :class:`~sphinxcontrib.bibtex.nodes.bibliography` node into a
     list of citations.
     """
     env = self.document.settings.env
     docname = env.docname
     for bibnode in self.document.traverse(bibliography):
         id_ = bibnode['ids'][0]
         bibcache = env.bibtex_cache.get_bibliography_cache(
             docname=docname, id_=id_)
         entries = env.bibtex_cache.get_bibliography_entries(
             docname=docname, id_=id_, warn=env.app.warn)
         # locate and instantiate style and backend plugins
         style = find_plugin('pybtex.style.formatting', bibcache.style)()
         backend = find_plugin('pybtex.backends', 'docutils')()
         # create citation nodes for all references
         if bibcache.list_ == "enumerated":
             nodes = docutils.nodes.enumerated_list()
             nodes['enumtype'] = bibcache.enumtype
             if bibcache.start >= 1:
                 nodes['start'] = bibcache.start
                 env.bibtex_cache.set_enum_count(
                     env.docname, bibcache.start)
             else:
                 nodes['start'] = env.bibtex_cache.get_enum_count(
                     env.docname)
         elif bibcache.list_ == "bullet":
             nodes = docutils.nodes.bullet_list()
         else:  # "citation"
             nodes = docutils.nodes.paragraph()
         # remind: style.format_entries modifies entries in unpickable way
         for entry in style.format_entries(entries):
             if bibcache.list_ in ["enumerated", "bullet"]:
                 citation = docutils.nodes.list_item()
                 citation += backend.paragraph(entry)
             else:  # "citation"
                 citation = backend.citation(entry, self.document)
                 # backend.citation(...) uses entry.key as citation label
                 # we change it to entry.label later onwards
                 # but we must note the entry.label now;
                 # at this point, we also already prefix the label
                 key = citation[0].astext()
                 bibcache.labels[key] = bibcache.labelprefix + entry.label
             node_text_transform(citation, transform_url_command)
             if bibcache.curly_bracket_strip:
                 node_text_transform(
                     citation,
                     transform_curly_bracket_strip)
             nodes += citation
             if bibcache.list_ == "enumerated":
                 env.bibtex_cache.inc_enum_count(env.docname)
         bibnode.replace_self(nodes)
예제 #18
0
 def _test_input(self, plugin):
     parser = find_plugin('pybtex.database.input', plugin)(encoding='UTF-8')
     writer = find_plugin('pybtex.database.output', plugin)(encoding='UTF-8')
     stream = BytesIO()
     writer_stream = TextIOWrapper(stream, 'UTF-8') if writer.unicode_io else stream
     parser_stream = TextIOWrapper(stream, 'UTF-8') if parser.unicode_io else stream
     writer.write_stream(self.reference_data, writer_stream)
     writer_stream.flush()
     stream.seek(0)
     parser.parse_stream(parser_stream)
     loaded_data = parser.data
     self.assertEqual(loaded_data, self.reference_data)
예제 #19
0
def convert(from_filename, to_filename,
        from_format=None, to_format=None,
        input_encoding=None, output_encoding=None,
        parser_options=None):
    if parser_options is None:
        parser_options = {}
    input_format = find_plugin('pybtex.database.input', name=from_format, filename=from_filename)
    output_format = find_plugin('pybtex.database.output', name=to_format, filename=to_filename)
    
    if from_filename == to_filename:
        raise ConvertError('input and output file can not be the same')

    bib_data = input_format(input_encoding, **parser_options).parse_file(from_filename)
    output_format(output_encoding).write_file(bib_data, to_filename)
예제 #20
0
    def run(self):

        style = find_plugin('pybtex.style.formatting', self.options.get('style', 'unsrt'))()
        parser = Parser()

        # Sort the publication entries by year reversed
        data = sorted(parser.parse_file(self.arguments[0]).entries.items(),
                      key=lambda e: e[1].fields['year'], reverse=True)
        _, entries = zip(*data)

        html = '<div class = "publication-list">\n'
        cur_year = None

        for entry in entries:
            # print a year title when year changes
            if entry.fields['year'] != cur_year:
                if cur_year is not None:  # not first year group
                    html += '</ul>'
                cur_year = entry.fields['year']
                html += '<h3>{}</h3>\n<ul>'.format(cur_year)

            html += '<li class = "publication">{}</li>'.format(
                list(style.format_entries((entry,)))[0].text.render_as('html'))

        if len(entries) != 0:  # publication list is nonempty
            html += '</ul>'

        html += '</div>'

        return [nodes.raw('', html, format='html'), ]
예제 #21
0
파일: __main__.py 프로젝트: rybesh/pybtex
    def run(self, options, args):
        from pybtex.plugin import find_plugin

        filename = args[0]
        ext = path.splitext(filename)[1]
        if ext != '.aux':
            filename = path.extsep.join([filename, 'aux'])

        not_supported_by_bibtex = {
            'output_backend': 'output backends',
            'label_style': 'label styles',
            'name_style': 'name styles',
            'abbreviate_names': 'abbreviated names',
        }
        if options.style_language != 'python':
            for option, what_is_not_supported in not_supported_by_bibtex.items():
                if getattr(options, option):
                    self.opt_parser.error(
                        '%s are only supported by the Pythonic style engine (-l python)' % what_is_not_supported
                    )


        kwargs = {}
        if options.label_style:
            kwargs['label_style'] = find_plugin('pybtex.style.labels', options.label_style)
        if options.name_style:
            kwargs['name_style'] = find_plugin('pybtex.style.names', options.name_style)
        if options.output_backend:
            kwargs['output_backend'] = find_plugin('pybtex.backends', options.output_backend)
        if options.bib_format:
            kwargs['bib_format'] = find_plugin('pybtex.database.input', options.bib_format)
        kwargs['abbreviate_names'] = bool(options.abbreviate_names)
        kwargs['min_crossrefs'] = options.min_crossrefs
        for option in ('bib_encoding', 'output_encoding', 'bst_encoding'):
            value = getattr(options, option) or options.encoding
            if value:
                kwargs[option] = value

        if options.style_language == 'bibtex':
            from pybtex import bibtex as engine
        elif options.style_language == 'python':
            import pybtex as engine
        else:
            self.opt_parser.error('unknown style language %s' % options.style_language)

        engine.make_bibliography(filename, **kwargs)
예제 #22
0
    def to_string(self, bib_format, **kwargs):
        """
        Return the data as a unicode string in the given format.

        :param bib_format: Data format ("bibtex", "yaml", etc.).

        """
        writer = find_plugin('pybtex.database.output', bib_format)(**kwargs)
        return writer.to_string(BibliographyData(entries={self.key: self}))
예제 #23
0
    def run(self):

        style = find_plugin('pybtex.style.formatting', self.options.get('style', 'unsrt'))()
        bibtex_dir = self.options.get('bibtex_dir', 'bibtex')
        highlight_author = self.options.get('highlight_author', None)

        parser = Parser()

        # Sort the publication entries by year reversed
        data = sorted(parser.parse_file(self.arguments[0]).entries.items(),
                      key=lambda e: e[1].fields['year'], reverse=True)

        print(type(data))
        html = '<div class = "publication-list">\n'
        cur_year = None

        if bibtex_dir:  # create the bibtex dir if the option is set
            try:
                os.mkdir(os.path.sep.join((self.output_folder, bibtex_dir)))
            except OSError:  # probably because the dir already exists
                pass

        for label, entry in data:
            # print a year title when year changes
            if entry.fields['year'] != cur_year:
                if cur_year is not None:  # not first year group
                    html += '</ul>'
                cur_year = entry.fields['year']
                html += '<h3>{}</h3>\n<ul>'.format(cur_year)

            pub_html = list(style.format_entries((entry,)))[0].text.render_as('html')
            if highlight_author:  # highlight an author (usually oneself)
                pub_html = pub_html.replace(highlight_author,
                                            '<strong>{}</strong>'.format(highlight_author), 1)
            html += '<li class = "publication">' + pub_html

            extra_links = ""
            if bibtex_dir:  # write bib files to bibtex_dir for downloading
                bib_link = '{}/{}.bib'.format(bibtex_dir, label)
                bib_data = BibliographyData(dict({label: entry}))
                bib_data.to_file('/'.join([self.output_folder, bib_link]), 'bibtex')
                extra_links += '[<a href="{}">bibtex</a>] '.format(bib_link)

            if 'pdf' in entry.fields:  # the link to the pdf file
                extra_links += '[<a href="{}">pdf</a>] '.format(entry.fields['pdf'])

            if extra_links:
                html += '<br/>' + extra_links

            html += '</li>'

        if len(data) != 0:  # publication list is nonempty
            html += '</ul>'

        html += '</div>'

        return [nodes.raw('', html, format='html'), ]
예제 #24
0
def make_bibliography(aux_filename,
                      bib_format=None,
                      bib_encoding=None,
                      output_encoding=None,
                      output_backend=None,
                      min_crossrefs=2,
                      **kwargs):
    """This functions extracts all nessessary information from .aux file
    and writes the bibliography.
    """

    from os import path
    from pybtex import auxfile
    from pybtex.plugin import find_plugin
    from pybtex.style import FormattedBibliography

    filename = path.splitext(aux_filename)[0]
    aux_data = auxfile.parse_file(aux_filename, output_encoding)

    output_backend = find_plugin('pybtex.backends', output_backend)
    bib_parser = find_plugin('pybtex.database.input', bib_format)
    bib_data = bib_parser(
        encoding=bib_encoding,
        wanted_entries=aux_data.citations,
        min_crossrefs=min_crossrefs,
    ).parse_files(aux_data.data, bib_parser.get_default_suffix())

    style_cls = find_plugin('pybtex.style.formatting', aux_data.style)
    style = style_cls(
        label_style=kwargs.get('label_style'),
        name_style=kwargs.get('name_style'),
        sorting_style=kwargs.get('sorting_style'),
        abbreviate_names=kwargs.get('abbreviate_names'),
    )
    citations = bib_data.add_extra_citations(aux_data.citations, min_crossrefs)
    entries = (bib_data.entries[key] for key in citations)
    formatted_entries = style.format_entries(entries)
    del entries
    formatted_bibliography = FormattedBibliography(formatted_entries, style)

    output_filename = filename + output_backend.get_default_suffix()
    output_backend(output_encoding).write_to_file(formatted_bibliography,
                                                  output_filename)
예제 #25
0
def make_bibliography(aux_filename,
        bib_format=None,
        bib_encoding=None,
        output_encoding=None,
        output_backend=None,
        min_crossrefs=2,
        **kwargs
        ):
    """This functions extracts all nessessary information from .aux file
    and writes the bibliography.
    """

    from os import path
    from pybtex import auxfile
    from pybtex.plugin import find_plugin
    from pybtex.style import FormattedBibliography

    filename = path.splitext(aux_filename)[0]
    aux_data = auxfile.parse_file(aux_filename, output_encoding)

    output_backend = find_plugin('pybtex.backends', output_backend)
    bib_parser = find_plugin('pybtex.database.input', bib_format)
    bib_data = bib_parser(
        encoding=bib_encoding,
        wanted_entries=aux_data.citations,
        min_crossrefs=min_crossrefs,
    ).parse_files(aux_data.data, bib_parser.get_default_suffix())

    style_cls = find_plugin('pybtex.style.formatting', aux_data.style)
    style = style_cls(
            label_style=kwargs.get('label_style'),
            name_style=kwargs.get('name_style'),
            sorting_style=kwargs.get('sorting_style'),
            abbreviate_names=kwargs.get('abbreviate_names'),
    )
    citations = bib_data.add_extra_citations(aux_data.citations, min_crossrefs)
    entries = (bib_data.entries[key] for key in citations)
    formatted_entries = style.format_entries(entries)
    del entries
    formatted_bibliography = FormattedBibliography(formatted_entries, style)

    output_filename = filename + output_backend.get_default_suffix()
    output_backend(output_encoding).write_to_file(formatted_bibliography, output_filename)
예제 #26
0
    def to_bytes(self, bib_format, **kwargs):
        """
        Return the data as a byte string in the given format.

        :param bib_format: Data format ("bibtex", "yaml", etc.).

        .. versionadded:: 0.19
        """
        writer = find_plugin('pybtex.database.output', bib_format)(**kwargs)
        return writer.to_bytes(self)
예제 #27
0
    def to_bytes(self, bib_format, **kwargs):
        """
        Return the data as a byte string in the given format.

        :param bib_format: Data format ("bibtex", "yaml", etc.).

        .. versionadded:: 0.19
        """
        writer = find_plugin('pybtex.database.output', bib_format)(**kwargs)
        return writer.to_bytes(self)
예제 #28
0
    def createHTML(self, parent, token, page):

        try:
            style = find_plugin('pybtex.style.formatting', token['bib_style'])
        except PluginNotFound:
            msg = 'Unknown bibliography style "{}".'
            raise exceptions.MooseDocsException(msg, token['bib_style'])

        citations = list(self.translator.getMetaData(page, 'citations'))
        formatted_bibliography = style().format_bibliography(self.extension.database, citations)
        html_backend = find_plugin('pybtex.backends', 'html')

        ol = html.Tag(parent, 'ol', class_='moose-bibliogrpahy')

        backend = html_backend(encoding='utf-8')
        for entry in formatted_bibliography:
            text = entry.text.render(backend)
            html.Tag(ol, 'li', id_=entry.key, string=text)

        return ol
예제 #29
0
def parse_bytes(value, bib_format, **kwargs):
    """
    Parse a byte string containing bibliography data and return a :py:class:`.BibliographyData` object.

    :param value: Byte string.
    :param bib_format: Data format (for example, "bibtexml").

    .. versionadded:: 0.19
    """

    parser = find_plugin('pybtex.database.input', bib_format)(**kwargs)
    return parser.parse_bytes(value)
예제 #30
0
def parse_string(value, bib_format, **kwargs):
    """
    Parse a Unicode string containing bibliography data and return a :py:class:`.BibliographyData` object.

    :param value: Unicode string.
    :param bib_format: Data format ("bibtex", "yaml", etc.).

    .. versionadded:: 0.19
    """

    parser = find_plugin('pybtex.database.input', bib_format)(**kwargs)
    return parser.parse_string(value)
예제 #31
0
 def _test_input(self, plugin):
     parser = find_plugin('pybtex.database.input', plugin)(encoding='UTF-8')
     writer = find_plugin('pybtex.database.output',
                          plugin)(encoding='UTF-8')
     stream = BytesIO()
     writer_stream = TextIOWrapper(stream,
                                   'UTF-8') if writer.unicode_io else stream
     parser_stream = TextIOWrapper(stream,
                                   'UTF-8') if parser.unicode_io else stream
     writer.write_stream(self.reference_data, writer_stream)
     writer_stream.flush()
     stream.seek(0)
     parser.parse_stream(parser_stream)
     loaded_data = parser.data
     self.assertEqual(loaded_data, self.reference_data)
     self.assertEqual(pickle.loads(pickle.dumps(loaded_data, 0)),
                      self.reference_data)
     self.assertEqual(pickle.loads(pickle.dumps(loaded_data, 1)),
                      self.reference_data)
     self.assertEqual(pickle.loads(pickle.dumps(loaded_data, 2)),
                      self.reference_data)
예제 #32
0
 def get_formatted_entries(
         self, bibliography_key: "BibliographyKey",
         docnames: List[str]) -> Iterable["FormattedEntry"]:
     """Get sorted bibliography entries along with their pybtex labels,
     with additional sorting and formatting applied from the pybtex style.
     """
     bibliography = self.bibliographies[bibliography_key]
     entries = dict(self.get_sorted_entries(bibliography_key, docnames))
     style = cast(
         "BaseStyle",
         find_plugin('pybtex.style.formatting', bibliography.style)())
     return style.format_entries(entries.values())
예제 #33
0
def parse_bytes(value, bib_format, **kwargs):
    """
    Parse a byte string containing bibliography data and return a :py:class:`.BibliographyData` object.

    :param value: Byte string.
    :param bib_format: Data format (for example, "bibtexml").

    .. versionadded:: 0.19
    """

    parser = find_plugin('pybtex.database.input', bib_format)(**kwargs)
    return parser.parse_bytes(value)
예제 #34
0
def parse_string(value, bib_format, **kwargs):
    """
    Parse a Unicode string containing bibliography data and return a :py:class:`.BibliographyData` object.

    :param value: Unicode string.
    :param bib_format: Data format ("bibtex", "yaml", etc.).

    .. versionadded:: 0.19
    """

    parser = find_plugin('pybtex.database.input', bib_format)(**kwargs)
    return parser.parse_string(value)
예제 #35
0
    def _pybtex_entries_to_markdown(entries):
        """Utility function to convert a BibTeX entries containing
        references into a Markdown string. Borrowed from propnet.
        """

        # TODO: replace this, very messy

        Pybtex_style = find_plugin("pybtex.style.formatting", "plain")()

        # hack so as to avoid messing with capitalization of formulae
        def format_title(self, e, which_field, as_sentence=True):
            formatted_title = field(which_field)
            if as_sentence:
                return sentence[formatted_title]
            else:
                return formatted_title

        Pybtex_style.format_title = format_title
        pybtex_style = Pybtex_style()

        Pybtex_md_backend = find_plugin("pybtex.backends", "markdown")

        # hack to not print labels (may remove this later)
        def write_entry(self, key, label, text):
            self.output("%s<splitkey>" % text)

        Pybtex_md_backend.write_entry = write_entry
        Pybtex_md_backend.symbols["newblock"] = "  \n>"
        pybtex_md_backend = Pybtex_md_backend()

        entries_formatted = pybtex_style.format_entries(entries.values())
        output = StringIO()
        pybtex_md_backend.write_to_stream(entries_formatted, output)

        # add blockquote style
        references_md = "  \n  \n".join(
            [f"> {md}  " for md in output.getvalue().split("<splitkey>")]
        )

        return references_md
예제 #36
0
파일: pybtexmod.py 프로젝트: mfa/webbib
def render(bib_filename):

    aux_filename="IMSfull.aux"
    bib_format=None
    bib_encoding=None
    output_encoding=None
    output_backend='html'
    min_crossrefs=2
    kwargs = {}
    auxstring = """
\\relax 
\\citation{*}
\\bibstyle{unsrt}
\\bibdata{%s}
""" % bib_filename

    aux_data = parse_auxfile_from_stream(auxstring, output_encoding)
    output_backend = find_plugin('pybtex.backends', output_backend)
    bib_parser = find_plugin('pybtex.database.input', bib_format)
    bib_data = bib_parser(
        encoding=bib_encoding,
        wanted_entries=aux_data.citations,
        min_crossrefs=min_crossrefs,
        ).parse_files(aux_data.data, bib_parser.get_default_suffix())
    
    style = MyStyle(
        label_style=kwargs.get('label_style'),
        name_style=kwargs.get('name_style'),
        sorting_style=kwargs.get('sorting_style'),
        abbreviate_names=kwargs.get('abbreviate_names'),
        )
    citations = bib_data.add_extra_citations(aux_data.citations, min_crossrefs)
    entries = (bib_data.entries[key] for key in citations)
    formatted_entries = style.format_entries(entries)

    return MyFormattedBibliography(formatted_entries, style)
예제 #37
0
def format_database(from_filename, to_filename,
        bib_format=None, output_backend=None,
        input_encoding=None, output_encoding=None,
        parser_options=None,
        min_crossrefs=2,
        style=None,
        **kwargs
    ):
    if parser_options is None:
        parser_options = {}
    input_format = find_plugin('pybtex.database.input', name=bib_format, filename=from_filename)
    output_backend = find_plugin('pybtex.backends', output_backend, filename=to_filename)
    
    bib_data = input_format(input_encoding, **parser_options).parse_file(from_filename)
    style_cls = find_plugin('pybtex.style.formatting', style)
    style = style_cls(
            label_style=kwargs.get('label_style'),
            name_style=kwargs.get('name_style'),
            sorting_style=kwargs.get('sorting_style'),
            abbreviate_names=kwargs.get('abbreviate_names'),
            min_crossrefs=min_crossrefs,
    )
    formatted_bibliography = style.format_bibliography(bib_data)
    output_backend(output_encoding).write_to_file(formatted_bibliography, to_filename)
예제 #38
0
파일: bibtex.py 프로젝트: FHilty/moose
    def createHTML(self, token, parent):

        try:
            style = find_plugin('pybtex.style.formatting', token.style)
        except PluginNotFound:
            msg = 'Unknown bibliography style "{}".'
            raise exceptions.RenderException(msg, token.style)

        citations = list()
        for tok in anytree.PreOrderIter(token.root):
            if isinstance(tok, BibtexCite):
                citations.extend(tok.keys)

        formatted_bibliography = style().format_bibliography(self.extension.database, citations)
        html_backend = find_plugin('pybtex.backends', 'html')

        ol = html.Tag(parent, 'ol', class_='moose-bibliogrpahy')

        backend = html_backend(encoding='utf-8')
        for entry in formatted_bibliography:
            text = entry.text.render(backend)
            html.Tag(ol, 'li', id_=entry.key, string=text)

        return ol
예제 #39
0
    def to_file(self, file, bib_format=None, **kwargs):
        """
        Save the data to a file.

        :param file: A file name or a file-like object.
        :param bib_format: Data format ("bibtex", "yaml", etc.).
            If not specified, Pybtex will try to guess by the file name.

        .. versionadded:: 0.19
        """
        if isinstance(file, basestring):
            filename = file
        else:
            filename = getattr(file, 'name', None)
        writer = find_plugin('pybtex.database.output', bib_format, filename=filename)(**kwargs)
        return writer.write_file(self, file)
예제 #40
0
    def to_file(self, file, bib_format=None, **kwargs):
        """
        Save the data to a file.

        :param file: A file name or a file-like object.
        :param bib_format: Data format ("bibtex", "yaml", etc.).
            If not specified, Pybtex will try to guess by the file name.

        .. versionadded:: 0.19
        """
        if isinstance(file, six.string_types):
            filename = file
        else:
            filename = getattr(file, 'name', None)
        writer = find_plugin('pybtex.database.output', bib_format, filename=filename)(**kwargs)
        return writer.write_file(self, file)
예제 #41
0
class FootCiteRole(XRefRole):
    """Class for processing the :rst:role:`footcite` role."""

    backend = find_plugin('pybtex.backends', 'docutils')()

    def result_nodes(
        self, document: "docutils.nodes.document", env: "BuildEnvironment",
        node: "docutils.nodes.Element", is_ref: bool
    ) -> Tuple[List["docutils.nodes.Node"],
               List["docutils.nodes.system_message"]]:
        """Transform node into footnote references, and
        add footnotes to a node stored in the environment's temporary data
        if they are not yet present.

        .. seealso::

           The node containing all footnotes is inserted into the document by
           :meth:`.foot_directives.FootBibliographyDirective.run`.
        """
        domain = cast("BibtexDomain", self.env.get_domain('cite'))
        keys = [key.strip() for key in self.target.split(',')]  # type: ignore
        try:
            foot_bibliography = env.temp_data["bibtex_foot_bibliography"]
        except KeyError:
            env.temp_data["bibtex_foot_bibliography"] = foot_bibliography = \
                domain.footbibliography_header.deepcopy()
        foot_old_refs = env.temp_data.setdefault("bibtex_foot_old_refs", set())
        foot_new_refs = env.temp_data.setdefault("bibtex_foot_new_refs", set())
        style = find_plugin('pybtex.style.formatting',
                            self.config.bibtex_default_style)()
        ref_nodes = []
        for key in keys:
            entry = get_bibliography_entry(domain.bibfiles, key)
            if entry is not None:
                ref_nodes.append(
                    self.backend.footnote_reference(entry, document))
                if key not in (foot_old_refs | foot_new_refs):
                    formatted_entry = style.format_entry(label='', entry=entry)
                    footnote = self.backend.footnote(formatted_entry, document)
                    node_text_transform(footnote, transform_url_command)
                    foot_bibliography += footnote
                    foot_new_refs.add(key)
            else:
                logger.warning('could not find bibtex key "%s"' % key,
                               location=(env.docname, self.lineno))
        return ref_nodes, []
예제 #42
0
def parse_file(file, bib_format=None, **kwargs):
    """
    Read bibliography data from file and return a :py:class:`.BibliographyData` object.

    :param file: A file name or a file-like object.
    :param bib_format: Data format ("bibtex", "yaml", etc.).
        If not specified, Pybtex will try to guess by the file name.

    .. versionadded:: 0.19
    """

    if isinstance(file, basestring):
        filename = file
    else:
        filename = getattr(file, 'name', None)

    parser = find_plugin('pybtex.database.input', bib_format, filename=filename)(**kwargs)
    return parser.parse_file(file)
예제 #43
0
def parse_file(file, bib_format=None, **kwargs):
    """
    Read bibliography data from file and return a :py:class:`.BibliographyData` object.

    :param file: A file name or a file-like object.
    :param bib_format: Data format ("bibtex", "yaml", etc.).
        If not specified, Pybtex will try to guess by the file name.

    .. versionadded:: 0.19
    """

    if isinstance(file, six.string_types):
        filename = file
    else:
        filename = getattr(file, 'name', None)

    parser = find_plugin('pybtex.database.input', bib_format, filename=filename)(**kwargs)
    return parser.parse_file(file)
예제 #44
0
def parse_bib(filename, exclude_fields=None) -> List[BiblioEntry]:
    """

    :param filename:
    :param exclude_fields:
    :return: a list of BiblioEntry
    """
    STYLE = Minimalism(abbreviate_names=True)
    HTML = find_plugin('pybtex.backends', 'html')()

    bibliography = parse_file(filename)
    exclude_fields = exclude_fields or []

    if exclude_fields:
        for entry in bibliography.entries.values():
            for ef in exclude_fields:
                if ef in entry.fields.__dict__['_dict']:
                    del entry.fields.__dict__['_dict'][ef]

    formattedBib = STYLE.format_bibliography(bibliography)

    # crossref media coverage
    import yaml
    with open('content/media.yaml', 'r') as media_yaml:
        media = yaml.full_load(media_yaml)

    from collections import defaultdict
    media_indexed = defaultdict(list)
    for m in media:
        media_key = m['project'].lower()
        media_indexed[media_key].append({'venue': m['venue'], 'url': m['url']})

    output = []
    for idx, entry in enumerate(bibliography.entries.values()):

        media = None
        if 'mediakey' in entry.fields:
            mk = entry.fields['mediakey']
            media = media_indexed[mk.lower()]

        formatted_string = formattedBib.entries[idx].text.render(HTML)
        output.append(BiblioEntry(formatted_string, media))

    return output
예제 #45
0
파일: __init__.py 프로젝트: rybesh/pybtex
def make_bibliography(aux_filename,
        bib_format=None,
        bib_encoding=None,
        output_encoding=None,
        **kwargs
        ):
    """This functions extracts all nessessary information from .aux file
    and writes the bibliography.
    """

    from os import path
    from pybtex import auxfile
    from pybtex.plugin import find_plugin

    filename = path.splitext(aux_filename)[0]
    aux_data = auxfile.parse_file(aux_filename, output_encoding)

    if bib_format is None:
        from pybtex.database.input import bibtex as bib_format


    try:
        output_backend = kwargs['output_backend']
    except KeyError:
        from pybtex.backends.latex import Backend as output_backend


    bib_format = bib_format.Parser
    bib_data = bib_format(bib_encoding).parse_files(aux_data.data, bib_format.get_default_suffix())

    style_cls = find_plugin('pybtex.style.formatting', aux_data.style)
    style = style_cls(
            label_style=kwargs.get('label_style'),
            name_style=kwargs.get('name_style'),
            abbreviate_names=kwargs.get('abbreviate_names', True),
    )
    entries = ((key, bib_data.entries[key]) for key in aux_data.citations)
    formatted_entries = style.format_entries(entries)
    del entries

    output_filename = filename + output_backend.get_default_suffix()
    output_backend(output_encoding).write_bibliography(formatted_entries, output_filename)
예제 #46
0
class CiteRole(XRefRole):
    """Class for processing the :rst:role:`cite` role."""
    backend = find_plugin('pybtex.backends', 'docutils')()
    innernodeclass = docutils.nodes.inline

    def result_nodes(self, document, env, node, is_ref):
        """Associate the pending_xref with the cite domain,
        and note the cited citation keys.
        """
        node['refdomain'] = 'cite'
        document.note_explicit_target(node, node)  # for backrefs
        domain = cast("BibtexDomain", env.get_domain('cite'))
        domain.citation_refs.append(
            CitationRef(
                citation_ref_id=node['ids'][0],
                docname=env.docname,
                line=document.line,
                keys=[key.strip() for key in self.target.split(',')],
            ))
        return [node], []
예제 #47
0
class CiteRole(XRefRole):
    """Class for processing the :rst:role:`cite` role."""
    backend = find_plugin('pybtex.backends', 'docutils')()

    def result_nodes(self, document, env, node, is_ref):
        """Transform reference node into a citation reference,
        and note that the reference was cited.
        """
        key = node['reftarget']
        # Note that at this point, usually, env.bibtex_cache.bibfiles
        # is still empty because the bibliography directive may not
        # have been processed yet, so we cannot get the actual entry.
        # Instead, we simply fake an entry with the desired key, and
        # fix the label at doctree-resolved time. This happens in
        # process_citation_references.
        entry = pybtex.database.Entry(type_=None)
        entry.key = key
        refnode = self.backend.citation_reference(entry, document)
        env.bibtex_cache.add_cited(key, env.docname)
        return [refnode], []
예제 #48
0
    def render_as(self, backend_name):
        r"""
        Render this :py:class:`Text` into markup.
        This is a wrapper method that loads a formatting backend plugin
        and calls :py:meth:`Text.render`.

        >>> text = Text('Longcat is ', Tag('em', 'looooooong'), '!')
        >>> print(text.render_as('html'))
        Longcat is <em>looooooong</em>!
        >>> print(text.render_as('latex'))
        Longcat is \emph{looooooong}!
        >>> print(text.render_as('text'))
        Longcat is looooooong!

        :param backend_name: The name of the output backend (like ``"latex"`` or
            ``"html"``).

        """
        from pybtex.plugin import find_plugin
        backend_cls = find_plugin('pybtex.backends', backend_name)
        return self.render(backend_cls())
예제 #49
0
    def render_as(self, backend_name):
        r"""
        Render this :py:class:`Text` into markup.
        This is a wrapper method that loads a formatting backend plugin
        and calls :py:meth:`Text.render`.

        >>> text = Text('Longcat is ', Tag('em', 'looooooong'), '!')
        >>> print text.render_as('html')
        Longcat is <em>looooooong</em>!
        >>> print text.render_as('latex')
        Longcat is \emph{looooooong}!
        >>> print text.render_as('text')
        Longcat is looooooong!

        :param backend_name: The name of the output backend (like ``"latex"`` or
            ``"html"``).

        """
        from pybtex.plugin import find_plugin
        backend_cls = find_plugin('pybtex.backends', backend_name)
        return self.render(backend_cls())
예제 #50
0
class CiteRole(XRefRole):
    """Class for processing the :rst:role:`footcite` role."""

    backend = find_plugin('pybtex.backends', 'docutils')()

    def make_refnode(self, document, env, key):
        cited = env.footbib_cache.cited[env.docname]
        for otherkeys in cited.values():
            if key in otherkeys:
                break
        else:
            cited[env.footbib_cache.current_id[env.docname]].add(key)
        # TODO get the actual entry
        return self.backend.footnote_reference(_fake_entry(key), document)

    def result_nodes(self, document, env, node, is_ref):
        """Transform reference node into a footnote reference,
        and note that the reference was cited.
        """
        keys = node['reftarget'].split(',')
        return [self.make_refnode(document, env, key) for key in keys], []
예제 #51
0
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
SITE_ROOT = os.path.dirname(SCRIPT_DIR)
INFILE = os.path.join(SCRIPT_DIR, 'publications.bib')
OUTFILE = os.path.join(SITE_ROOT, '_data', 'publications.json')

# Retrieves the year for a BibTeX entry
def getYear(entry):
    return int(entry.fields['year'])


# Read the BibTeX database file
db = parse_file(INFILE)

# Retrieve the Style class for the "plain" style
PlainStyle = find_plugin('pybtex.style.formatting', 'plain')
style = PlainStyle()

# Retrieve the HTML backend
HtmlBackend = find_plugin('pybtex.backends', 'html')
backend = HtmlBackend('utf-8')

# Sort the BibTeX database entries in reverse-chronological order
entries = list(db.entries.values())
entries = list(reversed(sorted(entries, key = getYear)))

# Retrieve the list of years that have publications
years = list([getYear(entry) for entry in entries])

# Create a blank list to hold the HTML for the entries for each year
publicationsForYears = {}
def test_pybtex_find_plugin():
    from pybtex.plugin import find_plugin
    assert (
        find_plugin("pybtex.backends", "docutils") is pybtex_docutils.Backend)
예제 #53
0
파일: cmdline.py 프로젝트: rybesh/pybtex
def check_plugin(option, option_string, value):
    return find_plugin(option.plugin_group, value)
예제 #54
0
from htmldiv import clean

INPUT_ENCODING = 'utf8'
OUTPUT_ENCODING = 'utf8'
BIB_STYLE = 'plain'  # this may also be 'unsrt'
HTML_CLASS = 'bibentry'

buf = StringIO()

# get the required objects:
# - parser (to read the bibtex file),
# - style (to format the entry as "richtext"),
# - backend (to write the "richtext" as html)

parser = find_plugin('pybtex.database.input', 'bibtex')(encoding = INPUT_ENCODING)
style = find_plugin('pybtex.style.formatting', BIB_STYLE)()
backend = find_plugin('pybtex.backends', 'htmldiv')(OUTPUT_ENCODING, HTML_CLASS)

# read the bib file and extract the wanted entry

biblio = parser.parse_file('example.bib')
entry = biblio.entries['key']

# format the entry as unsrt and using htmldiv backend

formatted_entry = style.format_entries((entry,))
formatted_bibliography = FormattedBibliography(formatted_entry, style)
backend.write_to_stream(formatted_bibliography, buf)

print clean(buf.getvalue())
예제 #55
0
 def apply(self):
     """Transform each
     :class:`~sphinxcontrib.bibtex.nodes.bibliography` node into a
     list of citations.
     """
     env = self.document.settings.env
     for bibnode in self.document.traverse(bibliography):
         # get the information of this bibliography node
         # by looking up its id in the bibliography cache
         id_ = bibnode['ids'][0]
         infos = [info for other_id, info
                  in env.bibtex_cache.bibliographies.iteritems()
                  if other_id == id_ and info.docname == env.docname]
         if not infos:
             raise RuntimeError(
                 "document %s has no bibliography nodes with id '%s'"
                 % (env.docname, id_))
         elif len(infos) >= 2:
             raise RuntimeError(
                 "document %s has multiple bibliography nodes with id '%s'"
                 % (env.docname, id_))
         info = infos[0]
         # generate entries
         entries = OrderedDict()
         for bibfile in info.bibfiles:
             # XXX entries are modified below in an unpickable way
             # XXX so fetch a deep copy
             data = env.bibtex_cache.bibfiles[bibfile].data
             if info.cite == "all":
                 bibfile_entries = data.entries.itervalues()
             elif info.cite == "cited":
                 bibfile_entries = (
                     entry for entry in data.entries.itervalues()
                     if env.bibtex_cache.is_cited(entry.key))
             elif info.cite == "notcited":
                 bibfile_entries = (
                     entry for entry in data.entries.itervalues()
                     if not env.bibtex_cache.is_cited(entry.key))
             else:
                 raise RuntimeError("invalid cite option (%s)" % info.cite)
             for entry in bibfile_entries:
                 entries[entry.key] = copy.deepcopy(entry)
         # order entries according to which were cited first
         # first, we add all keys that were cited
         # then, we add all remaining keys
         sorted_entries = []
         for key in env.bibtex_cache.get_all_cited_keys():
             try:
                 entry = entries.pop(key)
             except KeyError:
                 pass
             else:
                 sorted_entries.append(entry)
         sorted_entries += entries.itervalues()
         # locate and instantiate style plugin
         style_cls = find_plugin(
             'pybtex.style.formatting', info.style)
         style = style_cls()
         # create citation nodes for all references
         backend = output_backend()
         if info.list_ == "enumerated":
             nodes = docutils.nodes.enumerated_list()
             nodes['enumtype'] = info.enumtype
             if info.start >= 1:
                 nodes['start'] = info.start
                 env.bibtex_cache.set_enum_count(env.docname, info.start)
             else:
                 nodes['start'] = env.bibtex_cache.get_enum_count(env.docname)
         elif info.list_ == "bullet":
             nodes = docutils.nodes.bullet_list()
         else: # "citation"
             nodes = docutils.nodes.paragraph()
         # XXX style.format_entries modifies entries in unpickable way
         for entry in style.format_entries(sorted_entries):
             if info.list_ == "enumerated" or info.list_ == "bullet":
                 citation = docutils.nodes.list_item()
                 citation += entry.text.render(backend)
             else: # "citation"
                 citation = backend.citation(entry, self.document)
                 # backend.citation(...) uses entry.key as citation label
                 # we change it to entry.label later onwards
                 # but we must note the entry.label now;
                 # at this point, we also already prefix the label
                 key = citation[0].astext()
                 info.labels[key] = info.labelprefix + entry.label
             node_text_transform(citation, transform_url_command)
             if info.curly_bracket_strip:
                 node_text_transform(citation, transform_curly_bracket_strip)
             nodes += citation
             if info.list_ == "enumerated":
                 env.bibtex_cache.inc_enum_count(env.docname)
         bibnode.replace_self(nodes)
예제 #56
0
CATEGORIES = [
    'Journal Papers',
    'Conference Papers',
    'Workshop Papers',
    'Theses',
    'Technical Reports',
    'Patents',
]

arg_parser = argparse.ArgumentParser(description='Generates an HTML publication list from BibTeX.')
arg_parser.add_argument('input_file')
arg_parser.add_argument('--style', default='plain')
args = arg_parser.parse_args()
output_stream = sys.stdout

style_cls = find_plugin('pybtex.style.formatting', args.style)

style = JekyllStyle()
backend = JekyllBackend()

bib_parser = bibtex.Parser()
bib_file = bib_parser.parse_file(args.input_file)
entries = bib_file.entries.values()

# Sort the entries by date.
entries.sort(key=get_date_key, reverse=True)
formatted_entries = list(style.format_entries(entries))

# Group the entries by category.
categorized_pubs = collections.defaultdict(list)
for entry, formatted_entry in zip(entries, formatted_entries):
예제 #57
0
파일: bibtex.py 프로젝트: mangerij/moose
    def run(self, lines):
        """
        Create a bibliography from cite commands.
        """

        # Join the content to enable regex searches throughout entire text
        content = '\n'.join(lines)

        # Build the database of bibtex data
        self._citations = []              # member b/c it is used in substitution function
        self._bibtex = BibliographyData() # ""
        bibfiles = []
        match = re.search(self.RE_BIBLIOGRAPHY, content)
        if match:
            for bfile in match.group(1).split(','):
                try:
                    bibfiles.append(MooseDocs.abspath(bfile.strip()))
                    data = self.parseBibtexFile(bibfiles[-1])
                except UndefinedMacro:
                    LOG.error('Undefined macro in bibtex file: %s, specify macro_files arguments ' \
                              'in configuration file (e.g. website.yml)', bfile.strip())
                self._bibtex.add_entries(data.entries.iteritems())
        else:
            return lines

        # Determine the style
        match = re.search(self.RE_STYLE, content)
        if match:
            content = content.replace(match.group(0), '')
            try:
                style = find_plugin('pybtex.style.formatting', match.group(1))
            except PluginNotFound:
                LOG.error('Unknown bibliography style "%s"', match.group(1))
                return lines

        else:
            style = find_plugin('pybtex.style.formatting', 'plain')

        # Replace citations with author date, as an anchor
        content = re.sub(self.RE_CITE, self.authors, content)

        # Create html bibliography
        if self._citations:

            # Generate formatted html using pybtex
            formatted_bibliography = style().format_bibliography(self._bibtex, self._citations)
            backend = find_plugin('pybtex.backends', 'html')
            stream = io.StringIO()
            backend().write_to_stream(formatted_bibliography, stream)

            # Strip the bib items from the formatted html
            html = re.findall(r'\<dd\>(.*?)\</dd\>', stream.getvalue(),
                              flags=re.MULTILINE|re.DOTALL)

            # Produces an ordered list with anchors to the citations
            output = u'<ol class="moose-bibliography" data-moose-bibfiles="{}">\n'
            output = output.format(str(bibfiles))
            for i, item in enumerate(html):
                output += u'<li name="{}">{}</li>\n'.format(self._citations[i], item)
            output += u'</ol>\n'
            content = re.sub(self.RE_BIBLIOGRAPHY,
                             self.markdown.htmlStash.store(output, safe=True),
                             content)

        return content.split('\n')
예제 #58
0
 def apply(self):
     """Transform each
     :class:`~sphinxcontrib.bibtex.nodes.bibliography` node into a
     list of citations.
     """
     env = self.document.settings.env
     for bibnode in self.document.traverse(bibliography):
         # get the information of this bibliography node
         # by looking up its id in the bibliography cache
         id_ = bibnode['ids'][0]
         infos = [info for other_id, info
                  in env.bibtex_cache.bibliographies.iteritems()
                  if other_id == id_ and info.docname == env.docname]
         assert infos, "no bibliography id '%s' in %s" % (
             id_, env.docname)
         assert len(infos) == 1, "duplicate bibliography ids '%s' in %s" % (
             id_, env.docname)
         info = infos[0]
         # generate entries
         entries = OrderedDict()
         for bibfile in info.bibfiles:
             # XXX entries are modified below in an unpickable way
             # XXX so fetch a deep copy
             data = env.bibtex_cache.bibfiles[bibfile].data
             for entry in data.entries.itervalues():
                 visitor = FilterVisitor(
                     entry=entry,
                     is_cited=env.bibtex_cache.is_cited(entry.key))
                 try:
                     ok = visitor.visit(info.filter_)
                 except ValueError as e:
                     env.app.warn(
                         "syntax error in :filter: expression; %s" %
                         e)
                     # recover by falling back to the default
                     ok = env.bibtex_cache.is_cited(entry.key)
                 if ok:
                     entries[entry.key] = copy.deepcopy(entry)
         # order entries according to which were cited first
         # first, we add all keys that were cited
         # then, we add all remaining keys
         sorted_entries = []
         for key in env.bibtex_cache.get_all_cited_keys():
             try:
                 entry = entries.pop(key)
             except KeyError:
                 pass
             else:
                 sorted_entries.append(entry)
         sorted_entries += entries.itervalues()
         # locate and instantiate style and backend plugins
         style = find_plugin('pybtex.style.formatting', info.style)()
         backend = find_plugin('pybtex.backends', 'docutils')()
         # create citation nodes for all references
         if info.list_ == "enumerated":
             nodes = docutils.nodes.enumerated_list()
             nodes['enumtype'] = info.enumtype
             if info.start >= 1:
                 nodes['start'] = info.start
                 env.bibtex_cache.set_enum_count(env.docname, info.start)
             else:
                 nodes['start'] = env.bibtex_cache.get_enum_count(
                     env.docname)
         elif info.list_ == "bullet":
             nodes = docutils.nodes.bullet_list()
         else:  # "citation"
             nodes = docutils.nodes.paragraph()
         # XXX style.format_entries modifies entries in unpickable way
         for entry in style.format_entries(sorted_entries):
             if info.list_ == "enumerated" or info.list_ == "bullet":
                 citation = docutils.nodes.list_item()
                 citation += entry.text.render(backend)
             else:  # "citation"
                 citation = backend.citation(entry, self.document)
                 # backend.citation(...) uses entry.key as citation label
                 # we change it to entry.label later onwards
                 # but we must note the entry.label now;
                 # at this point, we also already prefix the label
                 key = citation[0].astext()
                 info.labels[key] = info.labelprefix + entry.label
             node_text_transform(citation, transform_url_command)
             if info.curly_bracket_strip:
                 node_text_transform(
                     citation,
                     transform_curly_bracket_strip)
             nodes += citation
             if info.list_ == "enumerated":
                 env.bibtex_cache.inc_enum_count(env.docname)
         bibnode.replace_self(nodes)