Пример #1
0
class CSLRenderer(object):
    """
        Interface with citeproc-py CSL library
    """
    def __init__(self, doc, style):
        self.citations={}
        self.doc=doc
        style=style.lower()

        bib_source = self.convertReferencesToJSON()
        bib_source = CiteProcJSON(bib_source)
        bib_style = CitationStylesStyle(os.path.join(CSL_PATH,style), validate=False)

        self.bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.html)
        self.prepareCitations()

    def convertReferencesToJSON(self):
        """
            Converts a SciDoc's references into the JSON format expected
        """
        def copyFields(dict1, dict2, field_list):
            for field in field_list:
                if field[0] in dict1:
                    dict2[field[1]]=dict1[field[0]]

        res=[]
        for ref in self.doc.references:
            newref={}
            copyFields(ref, newref,
                # new = old
                [("id","id"),
                ("title","title"),
                ("authors","author"),
                ("publisher","publisher-name"),
                ("title","title"),
                ("type","type"),
                ("URL","url"),
                ])
            newref["issued"]={"date-parts":[(ref.get("year","0"),)]}
            res.append(newref)
        return res

    def prepareCitations(self):
        """
        """
        for cit in self.doc.citations:
            self.citations[cit["id"]]=Citation([CitationItem(cit["ref_id"])])
            self.bibliography.register(self.citations[cit["id"]])

    def getCitationText(self, cit):
        """
        """
        warn=lambda x:None
        return self.bibliography.cite(self.citations[cit["id"]], warn)

    def getBibliography(self):
        """
            Returns the formatted bibliography
        """
        return [str(item) for item in self.bibliography.bibliography()]
Пример #2
0
def display_citation(bibtex_metadata, bib_stylename, formatter=formatter.html):
    # valid style names: plos, apa, pnas, nature, bmj, harvard1
    # full list is here: https://github.com/citation-style-language/styles

    bib_style = EnhancedCitationStyle(bib_stylename)
    bibliography = CitationStylesBibliography(bib_style, bibtex_metadata, formatter) #could be formatter.html
    id = "ITEM-1"
    citation = Citation([CitationItem(id)])
    bibliography.register(citation)

    citation_parts = "".join(bibliography.bibliography()[0])
    citation_text = "".join(citation_parts)

    if bib_stylename == 'apa':
        # strip extra periods and spaces that can occur in APA format
        citation_text = citation_text.replace('..', '.')
        citation_text = citation_text.replace('  ', ' ')

        citation_text = citation_text.strip()

        # strip leading comma
        if citation_text.startswith(','):
            citation_text = citation_text.lstrip(',').strip()

        citation_text = strip_duplicate_apa_title(bibtex_metadata, citation_text)

    citation_text = html.unescape(citation_text)

    return citation_text
Пример #3
0
def getReferencesFromJupyterNotebook(notebook):
    metadata = notebook.get('metadata')
    references = []
    bibliography = []
    filterReferences = []
    inline_references_table = dict()
    try:
        references = metadata.get('cite2c').get('citations')
        # logger.info("Loging references ---> {0}".format(references))
        bib_source = CiteProcJSON(references.values())
        bib_style = CitationStylesStyle(
            'jdhseo/styles/modern-language-association.csl', validate=False)
        bib = CitationStylesBibliography(bib_style, bib_source, formatter.html)
        # register citation
        for key, entry in bib_source.items():
            # exclude  "undefined" due to bug cite2c
            if key != "undefined":
                bib.register(Citation([CitationItem(key)]))
        for item in bib.bibliography():
            bibliography.append(str(item))
        for k, entry in references.items():
            inline_references_table[k] = getAuthorDateFromReference(entry)
    except Exception as e:
        logger.exception(e)
        pass
    # caseless matching
    #return references, sorted(bibliography, key=str.casefold), inline_references_table
    return references, sorted(bibliography,
                              key=lambda x: re.sub('[^A-Za-z]+', '', x).lower(
                              )), inline_references_table
Пример #4
0
class RFIC2009Paper(Document):
    rngschema = 'rfic.rng'
    namespace = 'http://www.mos6581.org/ns/rficpaper'

    def __init__(self, filename, bibliography_source):
        super().__init__(backend=pdf)
        self.styles = styles
        parser = xml_frontend.Parser(CustomElement,
                                     self.namespace,
                                     schema=self.rngschema)
        xml_tree = parser.parse(filename)
        self.root = xml_tree.getroot()
        bibliography_style = CitationStylesStyle('ieee.csl')
        self.bibliography = CitationStylesBibliography(bibliography_style,
                                                       bibliography_source,
                                                       csl_formatter)

        authors = [author.text for author in self.root.head.authors.author]
        if len(authors) > 1:
            self.author = ', '.join(authors[:-1]) + ', and ' + authors[-1]
        else:
            self.author = authors[0]
        self.title = self.root.head.title.text
        self.keywords = [term.text for term in self.root.head.indexterms.term]
        self.parse_input()

    def parse_input(self):
        self.title_par = Paragraph(self.title, style='title')
        self.author_par = Paragraph(self.author, style='author')
        self.affiliation_par = Paragraph(self.root.head.affiliation.text,
                                         style='affiliation')

        self.content = Chain(self)
        self.content << Abstract(self.root.head.abstract.text)
        self.content << IndexTerms(self.keywords)

        toc_heading = Heading('Table of Contents', style='unnumbered')
        toc = TableOfContents()
        self.content << rt.Section([toc_heading, toc])
        for element in self.root.body.getchildren():
            self.content << element.process()

        bib_heading = Heading('References', style='unnumbered')
        self.bibliography.sort()
        bib = Bibliography(self.bibliography)
        self.content << rt.Section([bib_heading, bib])

    def setup(self):
        self.page_count = 1
        page = RFICPage(self, first=True)
        self.add_page(page, self.page_count)

        page.title_box << self.title_par
        page.title_box << self.author_par
        page.title_box << self.affiliation_par

    def new_page(self, chains):
        page = RFICPage(self)
        self.page_count += 1
        self.add_page(page, self.page_count)
Пример #5
0
def format_bibliography(json_data):
    """ Format CSL-JSON to HTML APA format """
    json_data = _uniqueify(_flatten(json_data))
    bib_source = CiteProcJSON(json_data)
    style_path = get_style_filepath('apa')
    bib_style = CitationStylesStyle(style_path, validate=False)

    # Create the citeproc-py bibliography, passing it the:
    # * CitationStylesStyle,
    # * BibliographySource (CiteProcJSON in this case), and
    # * a formatter (plain, html, or you can write a custom formatter)

    bibliography = CitationStylesBibliography(bib_style, bib_source,
                                              formatter.html)

    # Processing citations in a document needs to be done in two passes as for
    # some CSL styles, a citation can depend on the order of citations in the
    # bibliography and thus on citations following the current one.
    # For this reason, we first need to register all citations with the
    # CitationStylesBibliography.

    for c in json_data:
        bibliography.register(Citation([CitationItem(c['id'])]))

    items = []
    for item in bibliography.bibliography():
        items.append(str(item))

    return items
Пример #6
0
def render_citation(node, style='apa'):
    """Given a node, return a citation"""
    csl = None
    if isinstance(node, PreprintService):
        csl = preprint_csl(node, node.node)
        data = [csl, ]
    else:
        data = [node.csl, ]

    bib_source = CiteProcJSON(data)

    custom = CUSTOM_CITATIONS.get(style, False)
    path = os.path.join(BASE_PATH, 'static', custom) if custom else os.path.join(CITATION_STYLES_PATH, style)
    bib_style = CitationStylesStyle(path, validate=False)

    bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.plain)

    citation = Citation([CitationItem(node._id)])

    bibliography.register(citation)

    bib = bibliography.bibliography()
    cit = unicode(bib[0] if len(bib) else '')

    title = csl['title'] if csl else node.csl['title']
    if cit.count(title) == 1:
        i = cit.index(title)
        prefix = clean_up_common_errors(cit[0:i])
        suffix = clean_up_common_errors(cit[i + len(title):])
        cit = prefix + title + suffix
    elif cit.count(title) == 0:
        cit = clean_up_common_errors(cit)

    return cit
Пример #7
0
class RFIC2009Paper(Document):
    rngschema = 'rfic.rng'
    namespace = 'http://www.mos6581.org/ns/rficpaper'

    def __init__(self, filename, bibliography_source):
        super().__init__(backend=pdf)
        self.styles = styles
        parser = xml_frontend.Parser(CustomElement, self.namespace,
                                     schema=self.rngschema)
        xml_tree = parser.parse(filename)
        self.root = xml_tree.getroot()
        bibliography_style = CitationStylesStyle('ieee.csl')
        self.bibliography = CitationStylesBibliography(bibliography_style,
                                                       bibliography_source,
                                                       csl_formatter)

        authors = [author.text for author in self.root.head.authors.author]
        if len(authors) > 1:
            self.author = ', '.join(authors[:-1]) + ', and ' + authors[-1]
        else:
            self.author = authors[0]
        self.title = self.root.head.title.text
        self.keywords = [term.text for term in self.root.head.indexterms.term]
        self.parse_input()

    def parse_input(self):
        self.title_par = Paragraph(self.title, style='title')
        self.author_par = Paragraph(self.author, style='author')
        self.affiliation_par = Paragraph(self.root.head.affiliation.text,
                                         style='affiliation')

        self.content = Chain(self)
        self.content << Abstract(self.root.head.abstract.text)
        self.content << IndexTerms(self.keywords)

        toc_heading = Heading('Table of Contents', style='unnumbered')
        toc = TableOfContents()
        self.content << rt.Section([toc_heading, toc])
        for element in self.root.body.getchildren():
            self.content << element.process()

        bib_heading = Heading('References', style='unnumbered')
        self.bibliography.sort()
        bib = Bibliography(self.bibliography)
        self.content << rt.Section([bib_heading, bib])

    def setup(self):
        self.page_count = 1
        page = RFICPage(self, first=True)
        self.add_page(page, self.page_count)

        page.title_box << self.title_par
        page.title_box << self.author_par
        page.title_box << self.affiliation_par

    def new_page(self, chains):
        page = RFICPage(self)
        self.page_count += 1
        self.add_page(page, self.page_count)
Пример #8
0
def render_citation(node, style='apa'):
    """Given a node, return a citation"""
    reformat_styles = ['apa', 'chicago-author-date', 'modern-language-association']
    csl = node.csl
    data = [csl, ]

    bib_source = CiteProcJSON(data)

    custom = CUSTOM_CITATIONS.get(style, False)
    path = os.path.join(BASE_PATH, 'static', custom) if custom else os.path.join(CITATION_STYLES_PATH, style)

    try:
        bib_style = CitationStylesStyle(path, validate=False)
    except ValueError:
        citation_style = CitationStyle.load(style)
        if citation_style is not None and citation_style.has_parent_style:
            parent_style = citation_style.parent_style
            parent_path = os.path.join(CITATION_STYLES_PATH, parent_style)
            bib_style = CitationStylesStyle(parent_path, validate=False)
        else:
            raise ValueError('Unable to find a dependent or independent parent style related to {}.csl'.format(style))

    bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.plain)

    citation = Citation([CitationItem(node._id)])

    bibliography.register(citation)

    bib = bibliography.bibliography()
    cit = unicode(bib[0] if len(bib) else '')

    title = csl['title'] if csl else node.csl['title']
    title = title.rstrip('.')
    if cit.count(title) == 1:
        i = cit.index(title)
        prefix = clean_up_common_errors(cit[0:i])
        suffix = clean_up_common_errors(cit[i + len(title):])
        if (style in reformat_styles):
            if suffix[0:1] == '.':
                suffix = suffix[1:]
            if title[-1] != '.':
                title += '.'
        cit = prefix + title + suffix
    elif cit.count(title) == 0:
        cit = clean_up_common_errors(cit)
        if (style in reformat_styles):
            cit = add_period_to_title(cit)

    if style == 'apa':
        cit = apa_reformat(node, cit)
    if style == 'chicago-author-date':
        cit = chicago_reformat(node, cit)
    if style == 'modern-language-association':
        cit = mla_reformat(node, cit)

    return cit
Пример #9
0
 def cite(self):
     style_path = get_style_filepath(CSL_STYLE)
     print(style_path)
     bib_style = CitationStylesStyle(style_path, validate=False)
     bibliography = CitationStylesBibliography(bib_style,
                                               CiteProcJSON([self.fields]),
                                               formatter.html)
     citation = Citation([CitationItem(self.instance.slug)])
     bibliography.register(citation)
     return str(bibliography.bibliography()[0])
Пример #10
0
def apa_html_utf8(json_data, alocale='en-US'):
    # CiteProcJSON receives a [{}] not a {}
    bib_source = CiteProcJSON([json_data])
    apa = CitationStylesStyle('apa', locale=alocale, validate=False)
    bibliography = CitationStylesBibliography(apa, bib_source, formatter.html)
    citation = Citation([CitationItem(json_data['id'])])
    bibliography.register(citation)

    # handle python weird string type
    return str(''.join(bibliography.bibliography()[0]).encode('utf-8'))
Пример #11
0
def _get_citation(library, document_id, style):

    bib_source = CiteProcJSON(library)
    bib_style = CitationStylesStyle(style)
    bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.plain)

    for id in range(0, len(document_id)):
        citation = Citation([CitationItem(library[id]['id'])])
        bibliography.register(citation)

    return bibliography.bibliography()
Пример #12
0
def make_citation_by_string(publications):
    bib_str = render_to_string('semsite/export/publications.bib',
                               {'publications': publications})
    bib = bibtex.BibTeX(io.StringIO(bib_str), encoding='utf-8')
    bib_style = CitationStylesStyle('gost-r-7-0-5-2008',
                                    'ru-ru',
                                    validate=False)
    bibliography = CitationStylesBibliography(bib_style, bib)
    citation_items = [CitationItem(key) for key in bib]
    citation = Citation(citation_items)
    bibliography.register(citation)
    return bibliography.cite(citation, warn)
Пример #13
0
    def __init__(self, filename):
        with open(filename, 'rt', encoding='UTF-8') as f:
            self.json_data = json.load(f)

        csl_io = io.BytesIO(utf_8_encode(self.json_data['csl'])[0])
        self.style = CitationStylesStyle(csl_io)
        self._fix_input(self.json_data['input'])
        self.references = [item['id'] for item in self.json_data['input']]
        self.references_dict = CiteProcJSON(self.json_data['input'])
        self.bibliography = CitationStylesBibliography(self.style,
                                                       self.references_dict)
        self.expected = self.json_data['result'].splitlines()
Пример #14
0
 def __init__(self, filename):
     self.csl_test = CslTest({}, None, (filename, ),
                             os.path.basename(filename))
     self.csl_test.parse()
     csl_io = io.BytesIO(utf_8_encode(self.data['csl'])[0])
     self.style = CitationStylesStyle(csl_io)
     self._fix_input(self.data['input'])
     self.references = [item['id'] for item in self.data['input']]
     self.references_dict = CiteProcJSON(self.data['input'])
     self.bibliography = CitationStylesBibliography(self.style,
                                                    self.references_dict)
     self.expected = self.data['result'].splitlines()
Пример #15
0
    def __init__(self,
                 for_cls,
                 csl_style,
                 export_format=adsFormatter.unicode,
                 journal_format=adsJournalFormat.default):
        """

        :param for_cls: input data for this class
        :param csl_style: export journal style
        :param export_format: export format
        """
        self.for_cls = for_cls
        self.csl_style = csl_style
        self.export_format = export_format
        self.journal_format = journal_format
        self.citation_item = []
        self.bibcode_list = []

        self.__update_title()

        # Process the JSON data to generate a citaproc-py BibliographySource.
        bib_source = CiteProcJSON(self.for_cls)

        csl_style_fullpath = os.path.realpath(__file__ + "/../../cslstyles")

        # load a CSL style (from the current directory)
        bib_style = CitationStylesStyle(os.path.join(csl_style_fullpath + '/' +
                                                     csl_style),
                                        validate=False)

        # Create the citaproc-py bibliography, passing it the:
        # * CitationStylesStyle,
        # * BibliographySource (CiteProcJSON in this case), and
        # * a formatter (plain, html, or you can write a custom formatter)
        # we are going to have CSL format everything using html and then format it as we need to match the
        # classic output
        self.bibliography = CitationStylesBibliography(bib_style, bib_source,
                                                       formatter.html)

        # Processing citations in a document needs to be done in two passes as for some
        # CSL styles, a citation can depend on the order of citations in the
        # bibliography and thus on citations following the current one.
        # For this reason, we first need to register all citations with the
        # CitationStylesBibliography.

        for item in self.for_cls:
            citation = Citation([CitationItem(item['id'])])
            self.citation_item.append(citation)
            self.bibliography.register(citation)
            # this is actually a bibcode that was passed in, but we have to use
            # one of CSLs predefined ids
            self.bibcode_list.append(''.join(item.get('locator', '')))
Пример #16
0
    def __init__(self, doc, style):
        self.citations = {}
        self.doc = doc
        style = style.lower()

        bib_source = self.convertReferencesToJSON()
        bib_source = CiteProcJSON(bib_source)
        bib_style = CitationStylesStyle(os.path.join(CSL_PATH, style),
                                        validate=False)

        self.bibliography = CitationStylesBibliography(bib_style, bib_source,
                                                       formatter.html)
        self.prepareCitations()
Пример #17
0
def generate_html(bib_csl_data, style_name='harvard1'):
    assert bib_csl_data
    bib_source = bib_csl_data
    bib_style = CitationStylesStyle(style_name, validate=False)

    bibliography = CitationStylesBibliography(
        bib_style, bib_source, formatter.html)

    bib_cites = [Citation([CitationItem(item)]) for item in bib_source]

    for item in bib_cites:
        bibliography.register(item)

    # FIXME: Can this be avoided?
    for item in bib_cites:
        bibliography.cite(item, _cite_warn)

    out = ''
    bibliography.sort()
    for item in bibliography.bibliography():
        line = '<p>' + ''.join(item) + '</p>\n'

        out += line

    return out
Пример #18
0
def generate_md(db, min_year=float('-inf'), max_year=float('inf'), group_by_year=True):

    directory = os.path.dirname(os.path.abspath(__file__))
    file_location = directory + os.path.sep + "static" + os.path.sep + "SasView_linktitle"
    bib_style = CitationStylesStyle(file_location, validate=False)

    # Create the citeproc-py bibliography, passing it the:
    # * CitationStylesStyle,
    # * BibliographySource (CiteProcJSON in this case), and
    # * a formatter (plain, html, or you can write a custom formatter)
    
    # add id to each item:
    items = db.items()
    for k, v in items:
        v['id'] = k
    
    bib_source = CiteProcJSON([v for k,v in items])
    year_lookup = sort_year(items)
    years = list(year_lookup.keys())
    years.sort()
    output_years = [year for year in years if year <= max_year and year >=min_year]
    year_cite_items = []
    year_link_items = []
    patent_items = []
    for year in output_years:
        year_link_items.append('[{0}](#{0})'.format(year))
        keys = year_lookup[year]
        keys.sort(key=lambda l: get_date_string(db[l]), reverse=True)
        bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.plain)
        citations = [Citation([CitationItem(k)]) for k in keys]
        for c in citations:
            bibliography.register(c)
        
        bib = bibliography.bibliography()
        cite_bib = []
        for k, b in zip(keys, bib):
            if db[k].get('type', None) == 'patent':
                patent_items.append(unescape("".join(b)))
            else:
                cite_bib.append(b)
        bib_output = [unescape("".join(b)) for b in cite_bib]
        year_output = ['## {0}\n'.format(year)]
        year_output.append('<small>[top](#acknowledgements-and-contacts)</small>\n')
        year_output.append('---\n')
        for i, bib_i in enumerate(bib_output):
            bib_final = ("{0}. " + bib_i).format(i)
            year_output.append(bib_final)
        year_cite_items.append("\n".join(year_output))
    return {"citations": year_cite_items, "links": year_link_items, "patents": patent_items}
Пример #19
0
    def serialize(self, pid, record, links_factory=None, **kwargs):
        """Serialize a single record.

        :param pid: Persistent identifier instance.
        :param record: Record instance.
        :param links_factory: Factory function for record links.
        """
        data = self.serializer.serialize(pid, record, links_factory)
        source = self._get_source(data)
        style = CitationStylesStyle(validate=False, **self._get_args(**kwargs))
        bib = CitationStylesBibliography(style, source, formatter.plain)
        citation = Citation([CitationItem(pid.pid_value)])
        bib.register(citation)

        return self._clean_result(''.join(bib.bibliography()[0]))
Пример #20
0
    def serialize(self, pid, record, links_factory=None, **kwargs):
        """Serialize a single record.

        :param pid: Persistent identifier instance.
        :param record: Record instance.
        :param links_factory: Factory function for record links.
        """
        data = self.serializer.serialize(pid, record, links_factory)
        source = self._get_source(data)
        style = CitationStylesStyle(validate=False, **self._get_args(**kwargs))
        bib = CitationStylesBibliography(style, source, formatter.plain)
        citation = Citation([CitationItem(pid.pid_value)])
        bib.register(citation)

        return self._clean_result(''.join(bib.bibliography()[0]))
Пример #21
0
def bibToCite(bibfile, ID):
    bib_source = BibTeX(bibfile)

    style_path = get_style_filepath('modern-language-association')
    bib_style = CitationStylesStyle(style_path, validate=False)

    bibliography = CitationStylesBibliography(bib_style, bib_source,
                                              formatter.plain)

    citation1 = Citation([CitationItem(ID)])

    bibliography.register(citation1)

    with open('out.txt', 'w') as f:
        for item in bibliography.bibliography():
            print(str(item), file=f)
Пример #22
0
class Bibliography:
    def __init__(self, bib_file, csl_file):
        self.bib_file = bib_file
        self.csl_file = csl_file
        self.bibtex = BibTeX(bib_file)
        self.style = CitationStylesStyle(csl_file, validate=False)
        self.bibliography = CitationStylesBibliography(bib_style, bib_source,formatter.plain)

    def citation(self, pandocname):
        c = Citation([CitationItem('whole-collection')])
        self.bibliography.register(c)
        return self.bibliography.cite(c)
    
    def bibliography(self):
        for item in self.bibliography.bibliography():
            str(item)
Пример #23
0
def to_CSL(project_data, citation_style, formatter_Style):
    """Format an OSF project as a citation.

    :param project_data: JSON-style dictionary of project information
    :param citation_style: File name of citation xml document
    :param formatter: Citeproc formatter (e.g. formatter.plain, formatter.html)
    """
    bib_source = CiteProcJSON([project_data])
    bib_style = CitationStylesStyle(citation_style)

    bibliography = CitationStylesBibliography(bib_style, bib_source, formatter_Style)

    citation1 = Citation([CitationItem(project_data['id'])])
    bibliography.register(citation1)

    return bibliography.bibliography()
Пример #24
0
def render_citation(node, style='apa'):
    """Given a node, return a citation"""
    csl = None
    if isinstance(node, PreprintService):
        csl = preprint_csl(node, node.node)
        data = [csl, ]
    else:
        data = [node.csl, ]

    bib_source = CiteProcJSON(data)

    custom = CUSTOM_CITATIONS.get(style, False)
    path = os.path.join(BASE_PATH, 'static', custom) if custom else os.path.join(CITATION_STYLES_PATH, style)
    bib_style = CitationStylesStyle(path, validate=False)

    bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.plain)

    citation = Citation([CitationItem(node._id)])

    bibliography.register(citation)

    bib = bibliography.bibliography()
    cit = unicode(bib[0] if len(bib) else '')

    title = csl['title'] if csl else node.csl['title']
    if cit.count(title) == 1:
        i = cit.index(title)
        prefix = clean_up_common_errors(cit[0:i])
        suffix = clean_up_common_errors(cit[i + len(title):])
        cit = prefix + title + suffix
    elif cit.count(title) == 0:
        cit = clean_up_common_errors(cit)

    if isinstance(node, PreprintService):
        cit_node = node.node
    else:
        cit_node = node

    if style == 'apa':
        cit = apa_reformat(cit_node, cit)
    if style == 'chicago-author-date':
        cit = chicago_reformat(cit_node, cit)
    if style == 'modern-language-association':
        cit = mla_reformat(cit_node, cit)

    return cit
Пример #25
0
def get_citation_string(json, id, style, locale):
    """Get the citation string from CiteProc library."""
    def _clean_result(text):
        """Remove double spaces, punctuation."""
        text = re.sub(r"\s\s+", " ", text)
        text = re.sub(r"\.\.+", ".", text)
        return text

    source = CiteProcJSON([json])
    citation_style = CitationStylesStyle(validate=False,
                                         style=style,
                                         locale=locale)
    bib = CitationStylesBibliography(citation_style, source, formatter.plain)
    citation = Citation([CitationItem(id)])
    bib.register(citation)

    return _clean_result(str(bib.bibliography()[0]))
Пример #26
0
def render_citation(node, style='apa'):
    """Given a node, return a citation"""
    if isinstance(node, Node):
        data = [
            node.csl,
        ]
    elif isinstance(node, PreprintService):
        csl = preprint_csl(node, node.node)
        data = [
            csl,
        ]
    else:
        raise ValueError

    bib_source = CiteProcJSON(data)

    bib_style = CitationStylesStyle(os.path.join(CITATION_STYLES_PATH, style),
                                    validate=False)

    bibliography = CitationStylesBibliography(bib_style, bib_source,
                                              formatter.plain)

    citation = Citation([CitationItem(node._id)])

    bibliography.register(citation)

    def warn(citation_item):
        pass

    bibliography.cite(citation, warn)
    bib = bibliography.bibliography()
    return unicode(bib[0] if len(bib) else '')
Пример #27
0
def display_citation(bibtex_metadata, bib_stylename, formatter=formatter.html):
    # valid style names: plos, apa, pnas, nature, bmj, harvard1
    # full list is here: https://github.com/citation-style-language/styles

    bib_style = EnhancedCitationStyle(bib_stylename)
    bibliography = CitationStylesBibliography(
        bib_style, bibtex_metadata, formatter)  #could be formatter.html
    id = "ITEM-1"
    citation = Citation([CitationItem(id)])
    bibliography.register(citation)

    citation_parts = u"".join(bibliography.bibliography()[0])
    citation_text = u"".join(citation_parts)

    html_parser = HTMLParser()
    citation_text = html_parser.unescape(citation_text)

    return citation_text
Пример #28
0
    def ama_html(self):
        if not self.citeproc_json:
            return ""

        bibliography = CitationStylesBibliography(
            CitationStylesStyle(
                str(
                    Path(__file__).parent / "styles" /
                    "american-medical-association-no-url.csl")),
            self.bib_source,
            formatter.html,
        )
        bibliography.register(Citation([CitationItem(self.doi)]))

        # The bibliography only contains 1 element
        citation = str(bibliography.bibliography()[0])
        citation = re.sub(r"^1\. ", "", citation)

        return clean(citation)
Пример #29
0
    def __init__(self, filename, bibliography_source):
        super().__init__(backend=pdf)
        parser = xml_frontend.Parser(CustomElement,
                                     self.namespace,
                                     schema=self.rngschema)
        xml_tree = parser.parse(filename)
        self.root = xml_tree.getroot()
        bibliography_style = CitationStylesStyle('ieee.csl')
        self.bibliography = CitationStylesBibliography(bibliography_style,
                                                       bibliography_source,
                                                       csl_formatter)

        authors = [author.text for author in self.root.head.authors.author]
        if len(authors) > 1:
            self.author = ', '.join(authors[:-1]) + ', and ' + authors[-1]
        else:
            self.author = authors[0]
        self.title = self.root.head.title.text
        self.keywords = [term.text for term in self.root.head.indexterms.term]
        self.parse_input()
Пример #30
0
    def __init__(self, doc, style):
        self.citations={}
        self.doc=doc
        style=style.lower()

        bib_source = self.convertReferencesToJSON()
        bib_source = CiteProcJSON(bib_source)
        bib_style = CitationStylesStyle(os.path.join(CSL_PATH,style), validate=False)

        self.bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.html)
        self.prepareCitations()
Пример #31
0
 def __init__(self, filename):
     self.csl_test = CslTest({}, None, (filename, ),
                             os.path.basename(filename))
     self.csl_test.parse()
     csl_io = io.BytesIO(utf_8_encode(self.data['csl'])[0])
     self.style = CitationStylesStyle(csl_io)
     self._fix_input(self.data['input'])
     self.references = [item['id'] for item in self.data['input']]
     self.references_dict = CiteProcJSON(self.data['input'])
     self.bibliography = CitationStylesBibliography(self.style,
                                                    self.references_dict)
     self.expected = self.data['result'].splitlines()
Пример #32
0
    def __init__(self, filename):
        with open(filename, encoding='UTF-8') as f:
            self.json_data = json.load(f)

        csl_io = io.BytesIO(utf_8_encode(self.json_data['csl'])[0])
        self.style = CitationStylesStyle(csl_io)
        self._fix_input(self.json_data['input'])
        self.references = [item['id'] for item in self.json_data['input']]
        self.references_dict = CiteProcJSON(self.json_data['input'])
        self.bibliography = CitationStylesBibliography(self.style,
                                                       self.references_dict)
        self.expected = self.json_data['result'].splitlines()
Пример #33
0
def render_citation(node, style='apa'):
    """Given a node, return a citation"""
    if isinstance(node, Node):
        data = [node.csl, ]
    elif isinstance(node, PreprintService):
        csl = preprint_csl(node, node.node)
        data = [csl, ]
    else:
        raise ValueError

    bib_source = CiteProcJSON(data)

    bib_style = CitationStylesStyle(os.path.join(CITATION_STYLES_PATH, style), validate=False)

    bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.plain)

    citation = Citation([CitationItem(node._id)])

    bibliography.register(citation)

    def warn(citation_item):
        pass

    bibliography.cite(citation, warn)
    bib = bibliography.bibliography()
    return unicode(bib[0] if len(bib) else '')
Пример #34
0
def print_using_citeproc(csl_json, keys, style):

    from citeproc import CitationStylesStyle, CitationStylesBibliography
    from citeproc import Citation, CitationItem
    from citeproc import formatter
    from citeproc.source.json import CiteProcJSON

    def warn(citation_item):
        raise RuntimeError(
            "Reference with key '{}' not found".format(citation_item.key)
        )

    bib_source = CiteProcJSON([csl_json])
    bib_style = CitationStylesStyle(style, validate=False)
    bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.html)
    citations = []
    # the following lines just do whatever example in citeproc repo does
    for key in keys:
        citation = Citation([CitationItem(key)])
        bibliography.register(citation)
        citations.append(citation)
    for citation in citations:
        # unused = bibliography.cite(citation, warn_missing_key)
        unused = bibliography.cite(citation, warn)
    for item in bibliography.bibliography():
        print(str(item))
Пример #35
0
def citeproc_csl(csl_json: Dict[str, Any],
                 style: str,
                 html: bool = False) -> str:
    """
    Renders a release entity to a styled citation.

    Notable styles include:
    - 'csl-json': special case to JSON encode the structured CSL object (via
      release_to_csl())
    - bibtext: multi-line bibtext format (used with LaTeX)

    Returns a string; if the html flag is set, and the style isn't 'csl-json'
    or 'bibtex', it will be HTML. Otherwise plain text.
    """
    if not csl_json.get("id"):
        csl_json["id"] = "unknown"
    if style == "csl-json":
        return json.dumps(csl_json)
    bib_src = CiteProcJSON([csl_json])
    form = formatter.plain
    if html:
        form = formatter.html
    style_path = get_style_filepath(style)
    bib_style = CitationStylesStyle(style_path, validate=False)
    bib = CitationStylesBibliography(bib_style, bib_src, form)
    bib.register(Citation([CitationItem(csl_json["id"])]))
    lines = bib.bibliography()[0]
    if style == "bibtex":
        out = ""
        for line in lines:
            if line.startswith(" @"):
                out += "@"
            elif line.startswith(" "):
                out += "\n " + line
            else:
                out += line
        return "".join(out)
    else:
        return "".join(lines)
Пример #36
0
    def apply(self):
        """Process Bib and Csl nodes, set up bibliography opbects, register and then cite the
        in-text citations, and then construct the bibliography."""
        # Find Bib and Csl nodes and extract the relevant information from them.
        bibnodes = [bib for bib in self.document.traverse() if isinstance(bib, nodes.Bib)]
        if bibnodes:
            self.bib_source = bibnodes[0]["bibfile"]

        cslnodes = [csl for csl in self.document.traverse() if isinstance(csl, nodes.Csl)]
        if cslnodes:
            self.bib_style = cslnodes[0]["cslfile"]

        # Remove the extraneous nodes now we're done with them.
        for node in bibnodes + cslnodes:
            node.parent.remove(node)

        citations = [cite for cite in self.document.traverse() \
                     if isinstance(cite, nodes.citation_reference)]

        if citations:
            # Set up the bibliography objects.
            self.bib_source = os.path.join(self.cwd, self.bib_source + \
                                          (".bib" if not self.bib_source.endswith('.bib') else ''))
            self.bib_source = BibTeX(self.bib_source)
            self.bib_style = os.path.join(self.cwd, self.bib_style + \
                                          (".csl" if not self.bib_style.endswith('.csl') else ''))
            self.bib_style = CitationStylesStyle(self.bib_style, validate=False)
            self.bibliography = CitationStylesBibliography(self.bib_style,
                                                           self.bib_source, formatter)

            # Register each in-text citation, then cite each one.
            for citation in citations:
                self.register_item(citation)
            for citation in citations:
                self.cite_item(citation)

            # Find the places where a Bibliography has been requested.
            bnodes = [bib for bib in self.document.traverse() \
                      if isinstance(bib, nodes.Bibliography)]
            self.document.b = self.bibliography
            biblist = nodes.enumerated_list()
            bibtexts = self.bibliography.bibliography()
            for btext in bibtexts:
                list_item = nodes.list_item()
                item_node = publish_doctree(escape(str(btext)))[0]
                list_item.append(item_node)
                biblist.append(list_item)
            for bnode in bnodes:
                bnode.parent.replace(bnode, biblist)
Пример #37
0
def parse_zotero_bibtex(items, citation_style_path):
	
	# separate the bibtex items and extract the key
	p = re.compile(ur'{([^,]+)')
	bibtexItems = []
	for item in items.split('@'):
		if len(item) > 1:
			# make a tuple containing bibtex key and bibtex string
			# splitting removed the @ of each entry so we need to add it back to get valid bibtex
			bibtexItems.append((re.search(p, item).group(1), "@" + item))

	# citeproc_py expects a bibtex file for input so we cannot just pass the bibtex string,
	# so we need to write it into a virtual file using StringIO
	output = StringIO.StringIO()
	output.write(items)
	output.seek(0)

	itemDicts = []
	try:
		# initialize citeproc-py
		bib_source = BibTeX(output)
		bib_style = CitationStylesStyle(citation_style_path, validate=False)
		
		# parse each bibtex entry into a html formatted bibliography entry
		# instead of generating a bibliography of all entries at once, create a
		# bibliography for each of the single items to retaing mapping to the 
		# corresponding bibtex string
		for (key, bibtex) in bibtexItems:
			bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.html)

			citationItem = CitationItem(key)
			citation = Citation([citationItem])
			
			bibliography.register(citation)
			bibliography.cite(citation, warning)
			
			renderedItem = ""

			# the rendered bibliography item returned by citeproc-py is split
			# inton an array of separate tokens, so we need to join them together 
			for token in bibliography.bibliography()[0]:
				renderedItem += token 

			itemDicts.append({
					'key': key,
					'bibtex': bibtex,
					'html': renderedItem
				})

	except:
		warning("ERROR PARSING")
		traceback.print_exc(file=sys.stderr)

	finally:
		output.close()
		return itemDicts
Пример #38
0
def run_citeproc_releases(args):
    for line in args.json_input:
        line = line.strip()
        if not line:
            continue
        entity = entity_from_json(line, ReleaseEntity, api_client=args.api.api_client)
        csl_json = release_to_csl(entity)
        # XXX:
        csl_json['id'] = "release:" + (entity.ident or "unknown")
        if args.style == "csl-json":
            args.json_output.write(json.dumps(csl_json) + "\n")
            continue
        bib_src = CiteProcJSON([csl_json])
        form = formatter.plain
        if args.html:
            form = formatter.html
        style_path = get_style_filepath(args.style)
        bib_style = CitationStylesStyle(style_path, validate=False)
        bib = CitationStylesBibliography(bib_style, bib_src, form)
        bib.register(Citation([CitationItem(csl_json['id'])]))
        # XXX:
        #args.json_output.write(
        #    json.dumps(release_to_csl(entity)) + '\n')
        lines = bib.bibliography()[0]
        if args.style == "bibtex":
            for l in lines:
                if l.startswith(" @"):
                    args.json_output.write("\n@")
                elif l.startswith(" "):
                    #print("line: START|{}|END".format(l))
                    args.json_output.write("\n  " + l)
                else:
                    args.json_output.write(l)
        else:
            args.json_output.write(''.join(lines) + "\n")
        print()
Пример #39
0
    def __init__(self, filename, bibliography_source):
        super().__init__(backend=pdf)
        parser = xml_frontend.Parser(CustomElement, self.namespace,
                                     schema=self.rngschema)
        xml_tree = parser.parse(filename)
        self.root = xml_tree.getroot()
        bibliography_style = CitationStylesStyle('ieee.csl')
        self.bibliography = CitationStylesBibliography(bibliography_style,
                                                       bibliography_source,
                                                       csl_formatter)

        authors = [author.text for author in self.root.head.authors.author]
        if len(authors) > 1:
            self.author = ', '.join(authors[:-1]) + ', and ' + authors[-1]
        else:
            self.author = authors[0]
        self.title = self.root.head.title.text
        self.keywords = [term.text for term in self.root.head.indexterms.term]
        self.parse_input()
Пример #40
0
def get_bib():
    bib_source = BibTeX('notebooks/bibliography.bib', encoding='utf8')
    bib_style = CitationStylesStyle('notebooks/springer.csl', validate=False)

    bibliography = CitationStylesBibliography(bib_style, bib_source,
                                              formatter.html)
    bib_cites = [Citation([CitationItem(item)]) for item in bib_source]

    for item in bib_cites:
        bibliography.register(item)
    for item in bib_cites:
        bibliography.cite(item, _cite_warn)

    num = len(bibliography.keys)
    bib_entries = dict()
    for i in range(num):
        bib = ''.join(bibliography.bibliography()[i])
        # remove beginning digits and \. from bib entries
        bib = '{}.&emsp;' + re.sub("^\d+\.", "", bib)
        bib_entries[bibliography.keys[i]] = bib

    return bib_entries
Пример #41
0
def render_citation(node, style='apa'):
    """Given a node, return a citation"""
    if isinstance(node, Node):
        data = [node.csl, ]
    elif isinstance(node, PreprintService):
        csl = preprint_csl(node, node.node)
        data = [csl, ]
    else:
        raise ValueError

    bib_source = CiteProcJSON(data)

    bib_style = CitationStylesStyle(os.path.join(CITATION_STYLES_PATH, style), validate=False)

    bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.plain)

    citation = Citation([CitationItem(node._id)])

    bibliography.register(citation)

    def warn(citation_item):
        pass

    bibliography.cite(citation, warn)
    bib = bibliography.bibliography()

    if len(bib):
        doi = data[0].get('DOI')
        if style == 'apa':
            first_segment = [list(bib[0])[0][:-2]]
            return ''.join(first_segment + list(bib[0])[1:13]) if doi else ''.join(first_segment + list(bib[0])[1:12] + list(bib[0])[13:])
        elif style == 'modern-language-association':
            return ''.join(list(bib[0])[:4] + ['.'] + list(bib[0])[4:5] + list(bib[0])[6:-2])
        elif style == 'chicago-author-date':
            return ''.join(list(bib[0])[0:3] + ['.'] + list(bib[0])[3:4] + [' '] + list(bib[0])[5:])
        else:
            return unicode(bib[0])
    else:
        return ''
Пример #42
0
def render_citation(node, style='apa'):
    """Given a node, return a citation"""
    data = [node.csl, ]

    bib_source = CiteProcJSON(data)

    bib_style = CitationStylesStyle(os.path.join(CITATION_STYLES_PATH, style), validate=False)

    bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.plain)

    citation = Citation([CitationItem(node._id)])

    bibliography.register(citation)

    def warn(citation_item):
        pass

    bibliography.cite(citation, warn)
    bib = bibliography.bibliography()
    return unicode(bib[0] if len(bib) else '')
Пример #43
0
def render_citation(node, style='apa'):
    """Given a node, return a citation"""
    data = [
        node.csl,
    ]

    bib_source = CiteProcJSON(data)

    bib_style = CitationStylesStyle(os.path.join(CITATION_STYLES_PATH, style),
                                    validate=False)

    bibliography = CitationStylesBibliography(bib_style, bib_source,
                                              formatter.plain)

    citation = Citation([CitationItem(node._id)])

    bibliography.register(citation)

    def warn(citation_item):
        pass

    bibliography.cite(citation, warn)
    bib = bibliography.bibliography()
    return unicode(bib[0] if len(bib) else '')
Пример #44
0
class Transform(docutils.transforms.Transform):
    """The PyCite Transform handles the recognition and processing of bib-backed citations"""
    default_priority = 208

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.cwd, _ = os.path.split(self.document.transformer.components['input'].source_path)
        self.bib_source = "bibliography"
        self.bib_style = "apa"
        self.bibliography = None
        self.citations = {}

    def register_item(self, item):
        """Registers the citation with the Bibliography"""
        citation = Citation([CitationItem(item["refname"])])
        self.citations[item["refname"]] = citation
        self.bibliography.register(citation)

    def cite_item(self, item):
        """Inserts the in-text citation"""
        try:
            i = publish_doctree(escape(self.bibliography.cite(self.citations[item["refname"]],
                                                              fail)))[0][0]
        except ValueError:
            return
        item.parent.replace(item, i)

    def apply(self):
        """Process Bib and Csl nodes, set up bibliography opbects, register and then cite the
        in-text citations, and then construct the bibliography."""
        # Find Bib and Csl nodes and extract the relevant information from them.
        bibnodes = [bib for bib in self.document.traverse() if isinstance(bib, nodes.Bib)]
        if bibnodes:
            self.bib_source = bibnodes[0]["bibfile"]

        cslnodes = [csl for csl in self.document.traverse() if isinstance(csl, nodes.Csl)]
        if cslnodes:
            self.bib_style = cslnodes[0]["cslfile"]

        # Remove the extraneous nodes now we're done with them.
        for node in bibnodes + cslnodes:
            node.parent.remove(node)

        citations = [cite for cite in self.document.traverse() \
                     if isinstance(cite, nodes.citation_reference)]

        if citations:
            # Set up the bibliography objects.
            self.bib_source = os.path.join(self.cwd, self.bib_source + \
                                          (".bib" if not self.bib_source.endswith('.bib') else ''))
            self.bib_source = BibTeX(self.bib_source)
            self.bib_style = os.path.join(self.cwd, self.bib_style + \
                                          (".csl" if not self.bib_style.endswith('.csl') else ''))
            self.bib_style = CitationStylesStyle(self.bib_style, validate=False)
            self.bibliography = CitationStylesBibliography(self.bib_style,
                                                           self.bib_source, formatter)

            # Register each in-text citation, then cite each one.
            for citation in citations:
                self.register_item(citation)
            for citation in citations:
                self.cite_item(citation)

            # Find the places where a Bibliography has been requested.
            bnodes = [bib for bib in self.document.traverse() \
                      if isinstance(bib, nodes.Bibliography)]
            self.document.b = self.bibliography
            biblist = nodes.enumerated_list()
            bibtexts = self.bibliography.bibliography()
            for btext in bibtexts:
                list_item = nodes.list_item()
                item_node = publish_doctree(escape(str(btext)))[0]
                list_item.append(item_node)
                biblist.append(list_item)
            for bnode in bnodes:
                bnode.parent.replace(bnode, biblist)
Пример #45
0
class ProcessorTest(object):
    def __init__(self, filename):
        with open(filename, encoding='UTF-8') as f:
            self.json_data = json.load(f)

        csl_io = io.BytesIO(utf_8_encode(self.json_data['csl'])[0])
        self.style = CitationStylesStyle(csl_io)
        self._fix_input(self.json_data['input'])
        self.references = [item['id'] for item in self.json_data['input']]
        self.references_dict = CiteProcJSON(self.json_data['input'])
        self.bibliography = CitationStylesBibliography(self.style,
                                                       self.references_dict)
        self.expected = self.json_data['result'].splitlines()

    @staticmethod
    def _fix_input(input_data):
        for i, ref in enumerate(input_data):
            if 'id' not in ref:
                ref['id'] = i
            if 'type' not in ref:
                ref['type'] = 'book'

    def execute(self):
        if self.json_data['citation_items']:
            citations = [self.parse_citation(item)
                         for item in self.json_data['citation_items']]
        elif self.json_data['citations']:
            citations = []
            for cit in self.json_data['citations']:
                cit = cit[0]
                citation_items = [self.parse_citation_item(cititem)
                                  for cititem in cit['citationItems']]
                citation = Citation(citation_items)
                citation.key = cit['citationID']
                citation.note_index = cit['properties']['noteIndex']
                citations.append(citation)
        elif self.json_data['bibentries']:
            citation_items = [self.parse_citation_item({'id': entry})
                              for entry in self.json_data['bibentries'][-1]]
            citations = [Citation(citation_items)]
        else:
            citation_items = [self.parse_citation_item({'id': ref})
                              for ref in self.references]
            citations = [Citation(citation_items)]

        for citation in citations:
            self.bibliography.register(citation)

        if self.style.has_bibliography():
            self.bibliography.sort()

        results = []
        do_nothing = lambda x: None     # callback passed to cite()
        if self.json_data['mode'] == 'citation':
            if self.json_data['citations']:
                for i, citation in enumerate(citations):
                    if i == len(citations) - 1:
                        dots_or_other = '>>'
                    else:
                        dots_or_other = '..'
                    results.append('{}[{}] '.format(dots_or_other, i) +
                                   self.bibliography.cite(citation, do_nothing))
            else:
                for citation in citations:
                    results.append(self.bibliography.cite(citation, do_nothing))
        elif self.json_data['mode'] in ('bibliography', 'bibliography-nosort'):
            results.append(self.bibliography.bibliography())

        return results

    def parse_citation(self, citation_data):
        citation_items = []
        for item in citation_data:
            citation_item = self.parse_citation_item(item)
            citation_items.append(citation_item)

        return Citation(citation_items)

    def parse_citation_item(self, citation_item_data):
        options = {}
        for key, value in citation_item_data.items():
            python_key = key.replace('-', '_')
            if python_key == 'id':
                reference_key = str(value)
                continue
            elif python_key == 'locator':
                try:
                    options['locator'] = Locator(citation_item_data['label'],
                                                 value)
                except KeyError:
                    # some tests don't specify the label
                    options['locator'] = Locator('page', value)
            elif python_key == 'label':
                pass
            else:
                options[python_key] = value

        return CitationItem(reference_key, **options)
Пример #46
0
# Parse the BibTeX database.

bib_source = BibTeX('xampl.bib')


# load a CSL style (from the current directory)

bib_style = CitationStylesStyle('harvard1', validate=False)


# Create the citeproc-py bibliography, passing it the:
# * CitationStylesStyle,
# * BibliographySource (BibTeX in this case), and
# * a formatter (plain, html, or you can write a custom formatter)

bibliography = CitationStylesBibliography(bib_style, bib_source,
                                          formatter.html)


# Processing citations in a document needs to be done in two passes as for some
# CSL styles, a citation can depend on the order of citations in the
# bibliography and thus on citations following the current one.
# For this reason, we first need to register all citations with the
# CitationStylesBibliography.

citation1 = Citation([CitationItem('whole-collection')])
citation2 = Citation([CitationItem('whole-set'), CitationItem('misc-full')])
citation3 = Citation([CitationItem('techreport-full')])
citation4 = Citation([CitationItem('mastersthesis-minimal')])
citation5 = Citation([CitationItem('inproceedings-full'),
                      CitationItem('unpublished-full')])
Пример #47
0
# Parse the BibTeX database.

bib_source = BibTeX('xampl.bib')


# load a CSL style (from the current directory)

bib_style = CitationStylesStyle('harvard1.csl')


# Create the citeproc-py bibliography, passing it the:
# * CitationStylesStyle,
# * BibliographySource (BibTeX in this case), and
# * a formatter (plain, html, or you can write a custom formatter)

bibliography = CitationStylesBibliography(bib_style, bib_source,
                                          formatter.plain)


# Processing citations in a document need to be done in two passes as for some
# CSL styles, a citation can depend on the order of citations in the
# bibliography and thus on citations following the current one.
# For this reason, we first need to register all citations with the
# CitationStylesBibliography.

citation1 = Citation([CitationItem('whole-collection')])
citation2 = Citation([CitationItem('whole-set'), CitationItem('misc-full')])
citation3 = Citation([CitationItem('techreport-minimal')])

bibliography.register(citation1)
bibliography.register(citation2)
bibliography.register(citation3)
Пример #48
0
# Parse the BibTeX database.

bib_source = BibTeX('xampl.bib')


# load a CSL style (from the current directory)

bib_style = CitationStylesStyle('harvard1', validate=False)


# Create the citeproc-py bibliography, passing it the:
# * CitationStylesStyle,
# * BibliographySource (BibTeX in this case), and
# * a formatter (plain, html, or you can write a custom formatter)

bibliography = CitationStylesBibliography(bib_style, bib_source,
                                          formatter.plain)


# Processing citations in a document needs to be done in two passes as for some
# CSL styles, a citation can depend on the order of citations in the
# bibliography and thus on citations following the current one.
# For this reason, we first need to register all citations with the
# CitationStylesBibliography.

citation1 = Citation([CitationItem('whole-collection')])
citation2 = Citation([CitationItem('whole-set'), CitationItem('misc-full')])
citation3 = Citation([CitationItem('techreport-full')])
citation4 = Citation([CitationItem('mastersthesis-minimal')])
citation5 = Citation([CitationItem('inproceedings-full'),
                      CitationItem('unpublished-full')])
Пример #49
0
    parse_file.closed
def write_to_txt(filename, bibliography):
    i=1
    f = open(filename,'w', encoding='utf8')
    for item in bibliography.bibliography():
        if(numbering_txt):
            f.write(unicode(str(i))+'. ')
            i=i+1
        f.write(unicode(str(item))+'\n')
    f.close()
def write_to_bbl(filename, bibliography):
    f = open(filename,'w', encoding='utf8')
    f.write('\\begin{thebibliography}{}\n')
    i=0
    for item in bibliography.bibliography():
        f.write('\\bibitem{'+my_cities[i]+'}')
        f.write(unicode(str(item))+'\n')
        i=i+1
    f.write('\\end{thebibliography}')
    f.close()

bib_source = BibTeX('literature.bib', encoding='utf8')
bib_style = CitationStylesStyle('gost-r-7-0-5-2008', locale='ru-RU', validate=False)
bibliography = CitationStylesBibliography(bib_style, bib_source, formatter.plain)

get_cities('literature.bib')
for i in range(len(my_cities)):
    bibliography.register(Citation([CitationItem(my_cities[i])]))

write_to_txt('my_bibliography.txt',bibliography)
write_to_bbl('my_bibliography.bbl',bibliography)
Пример #50
0
def bib(style_path,
        ds,
        return_cites_and_keys = False,
        formatter = "chocolate",
        dumb_quotes = True,
          # Turning this off won't educate any straight quotes in
          # the data, but leaving it on will stupefy all the
          # smart quotes in the output.
        apa_tweaks = True,
        # The below options are ignored unless apa_tweaks is on.
        always_include_issue = False,
        include_isbn = False,
        url_after_doi = False,
        publisher_website = True,
        abbreviate_given_names = True):

    if isinstance(formatter, str):
        try:             formatter = formatter_from_name[formatter]
        except KeyError: raise ValueError('Unknown formatter "{}"'.format(formatter))        

    style = get_style(style_path, apa_tweaks,
        include_isbn, url_after_doi, abbreviate_given_names)

    ds = deepcopy(ds)
    if apa_tweaks:
    # Distinguish entries that would have identical authors and years
    # by adding suffixes to the years.
        # Group works by author and year.
        ay = defaultdict(list)
        for d in ds:
            k = repr(d.get('author') or d.get('editor'))  + '/' + str(d['issued']['date-parts'][0][0])
            if not any(d is v for v in ay[k]):
                ay[k].append(d)
        # If any group has more than one element, add suffixes.
        for v in ay.values():
            if len(v) > 1:
                for i, d in enumerate(sorted(v, key = title_sort_key)):
                   d['year_suffix'] = ascii_lowercase[i]
    for d in ds:
        if 'id' not in d:
            d['id'] = str(random())
        for k in list(d.keys()):
            if d[k] is None: del d[k]
        if apa_tweaks:
            # By default, don't include the issue number for
            # journal articles.
            if not always_include_issue and d['type'] == 'article-journal':
                delf(d, 'issue')
            # Use the weird "Retrieved from Dewey, Cheatem, &
            # Howe website: http://example.com" format prescribed
            # for reports.
            if publisher_website and d['type'] == 'report' and 'publisher' in d and 'URL' in d:
                d['URL'] = '{} website: {}'.format(
                    d.pop('publisher'), d['URL'])
            # Add structure words for presentations and include
            # the event place.
            if d['type'] == 'speech' and d['genre'] == 'paper':
                d['event'] = 'meeting of the {}, {}'.format(
                    d.pop('publisher'), d['event-place'])
            if d['type'] == 'speech' and d['genre'] == 'video':
                d['medium'] = 'Video file'
                del d['genre']
            # When abbreviating given names, remove hyphens
            # preceding lowercase letters. Otherwise, weird
            # stuff happens.
            if abbreviate_given_names and 'author' in d:
               for a in d['author']:
                   if 'given' in a:
                       a['given'] = sub(
                           '-(.)',
                           lambda mo:
                               ("" if mo.group(1).islower() else "-") +
                               mo.group(1),
                           a['given'])
            # Abbreviate a long list of authors with an ellipsis
            # and the final author.
            if 'author' in d and len(d['author']) > 7:
                d['author'] = (
                    d['author'][0:6] +
                    [{'given': '', 'family': '⣥<ellipsis>⣥'}] +
                    d['author'][-1:])

    bibliography = CitationStylesBibliography(
        style,
        {ref.key: ref for ref in parse_references(ds)},
        formatter)
    cites = [ Citation([CitationItem(d['id'])]) for d in ds ]
    for c in cites: bibliography.register(c)
    def sort_key_f(item):
        ref = item.reference
        names = [(name['family'].lower(), name['given'][0].lower() if 'given' in name else '')
            for name in ref.get('author') or ref.get('editor')
            if name.family != '⣥<ellipsis>⣥']
        return (names, ref['issued']['year'],
            title_sort_key(ref),
            ref['page']['first'] if 'page' in ref else '')
    if len(ds) > 1:
        # Sort the bibliography
        # bibliography.sort()   # Doesn't appear to handle leading "the"s correctly.
        bibliography.items = sorted(bibliography.items, key = sort_key_f)
        bibliography.keys = [item.key for item in bibliography.items]
    bibl = bibliography.bibliography()

    for i, s in enumerate(bibl):
        s = ''.join(s)
        # Fix spacing and punctuation issues.
        s = s.replace('  ', ' ')
        s = sub(r'([.!?…])\.', r'\1', s)
        if dumb_quotes:
            s = s.replace('‘', "'").replace('’', "'").replace('“', '"').replace('”', '"')
        if apa_tweaks:
            if formatter is citeproc.formatter.html or formatter is chocolate:
                # Italicize the stuff between a journal name and a volume
                # number.
                s = sub(r'</i>, <i>(\d)', r', \1', s)
            # Make "p." into "pp." when more than one page is cited.
            s = sub(r'(\W)p\. (\S+[,–])', r'\1pp. \2', s)
            # Replace the ellipsis placeholder.
            s = s.replace('⣥<ellipsis>⣥, ., &', '…')
        bibl[i] = s

    if return_cites_and_keys:
        fcites = [bibliography.cite(c, lambda x: None) for c in cites]
        return (fcites, bibliography.keys, bibl)
    else:
        return bibl
Пример #51
0
    meta = doc[0]['unMeta']
    
    result = meta.get('bibliography', {})
    if result:
        bibliography_path = value_of_metadata(result)
    result = meta.get('csl', {})
    if result:
        csl_path = value_of_metadata(result)
    
    if bibliography_path == None or csl_path == None:
        raise Exception('Metadata variables must be set for both bibliography and csl.')
    
    # Parse the BibTeX database.
    bib_source = BibTeX(bibliography_path, encoding='utf-8')

    # load a CSL style
    bib_style = CitationStylesStyle(csl_path, validate=False)
    
    bibliography = CitationStylesBibliography(bib_style, bib_source, f)
        
    altered = walk(doc, citation_register, format, doc[0]['unMeta'])
    second = walk(altered, citation_replace, format, doc[0]['unMeta'])
    
    references = []
    for item, key in zip(bibliography.bibliography(), bibliography.keys):
        attrs = {'id': key, 'class':'h-cite'}
        references.append(Div(attributes(attrs),[Para([render(str(item))])]))
        
    second[1].extend(references) # add more paragraphs to the end of the main document list of blocks
    
    json.dump(second, sys.stdout)
Пример #52
0
class RFIC2009Paper(Document):
    rngschema = 'rfic.rng'
    namespace = 'http://www.mos6581.org/ns/rficpaper'

    def __init__(self, filename, bibliography_source):
        super().__init__(backend=pdf)
        parser = xml_frontend.Parser(CustomElement, self.namespace,
                                     schema=self.rngschema)
        xml_tree = parser.parse(filename)
        self.root = xml_tree.getroot()
        bibliography_style = CitationStylesStyle('ieee.csl')
        self.bibliography = CitationStylesBibliography(bibliography_style,
                                                       bibliography_source,
                                                       csl_formatter)

        authors = [author.text for author in self.root.head.authors.author]
        if len(authors) > 1:
            self.author = ', '.join(authors[:-1]) + ', and ' + authors[-1]
        else:
            self.author = authors[0]
        self.title = self.root.head.title.text
        self.keywords = [term.text for term in self.root.head.indexterms.term]
        self.parse_input()

    def parse_input(self):
        self.title_par = Paragraph(self.title, style=styles['title'])
        self.author_par = Paragraph(self.author, style=styles['author'])
        self.affiliation_par = Paragraph(self.root.head.affiliation.text,
                                         style=styles['affiliation'])

        self.content = Chain(self)
        self.content << Abstract(self.root.head.abstract.text)
        self.content << IndexTerms(self.keywords)

        self.content << Heading(self, 'Table of Contents',
                                style=styles['unnumbered'], level=1)
        toc = TableOfContents(style=styles['toc'],
                              styles=[styles['toc1'], styles['toc2'],
                                      styles['toc3']])
        self.content << toc
        for section in self.root.body.section:
            for flowable in section.process(self):
                toc.register(flowable)
                self.content << flowable
        try:
            for flowable in self.root.body.acknowledgement.process(self):
                toc.register(flowable)
                self.content << flowable
        except AttributeError:
            pass
        self.content << Heading(self, 'References', style=styles['unnumbered'],
                                level=1)
        self.bibliography.sort()
        self.content << self.bibliography.bibliography()

    def setup(self):
        self.page_count = 1
        page = RFICPage(self, first=True)
        self.add_page(page, self.page_count)

        page.title_box << self.title_par
        page.title_box << self.author_par
        page.title_box << self.affiliation_par

    def new_page(self, chains):
        page = RFICPage(self)
        self.page_count += 1
        self.add_page(page, self.page_count)
Пример #53
0
def render_citation(node, style='apa'):
    """Given a node, return a citation"""
    reformat_styles = [
        'apa', 'chicago-author-date', 'modern-language-association'
    ]
    csl = None
    if isinstance(node, PreprintService):
        csl = preprint_csl(node, node.node)
        data = [
            csl,
        ]
    else:
        data = [
            node.csl,
        ]

    bib_source = CiteProcJSON(data)

    custom = CUSTOM_CITATIONS.get(style, False)
    path = os.path.join(BASE_PATH, 'static',
                        custom) if custom else os.path.join(
                            CITATION_STYLES_PATH, style)

    try:
        bib_style = CitationStylesStyle(path, validate=False)
    except ValueError:
        citation_style = CitationStyle.load(style)
        if citation_style is not None and citation_style.has_parent_style:
            parent_style = citation_style.parent_style
            parent_path = os.path.join(CITATION_STYLES_PATH, parent_style)
            bib_style = CitationStylesStyle(parent_path, validate=False)
        else:
            raise ValueError(
                'Unable to find a dependent or independent parent style related to {}.csl'
                .format(style))

    bibliography = CitationStylesBibliography(bib_style, bib_source,
                                              formatter.plain)

    citation = Citation([CitationItem(node._id)])

    bibliography.register(citation)

    bib = bibliography.bibliography()
    cit = unicode(bib[0] if len(bib) else '')

    title = csl['title'] if csl else node.csl['title']
    title = title.rstrip('.')
    if cit.count(title) == 1:
        i = cit.index(title)
        prefix = clean_up_common_errors(cit[0:i])
        suffix = clean_up_common_errors(cit[i + len(title):])
        if (style in reformat_styles):
            if suffix[0:1] == '.':
                suffix = suffix[1:]
            if title[-1] != '.':
                title += '.'
        cit = prefix + title + suffix
    elif cit.count(title) == 0:
        cit = clean_up_common_errors(cit)
        if (style in reformat_styles):
            cit = add_period_to_title(cit)

    if isinstance(node, PreprintService):
        cit_node = node.node
    else:
        cit_node = node

    if style == 'apa':
        cit = apa_reformat(cit_node, cit)
    if style == 'chicago-author-date':
        cit = chicago_reformat(cit_node, cit)
    if style == 'modern-language-association':
        cit = mla_reformat(cit_node, cit)

    return cit
Пример #54
0
class ProcessorTest(object):
    """Parses atest fixture and provides a method for processing the tests
    defined in it."""
    bib_prefix = '<div class="csl-bib-body">'
    bib_suffix = '</div>'
    item_prefix = '  <div class="csl-entry">'
    item_suffix = '</div>'

    def __init__(self, filename):
        self.csl_test = CslTest({}, None, (filename, ),
                                os.path.basename(filename))
        self.csl_test.parse()
        csl_io = io.BytesIO(utf_8_encode(self.data['csl'])[0])
        self.style = CitationStylesStyle(csl_io)
        self._fix_input(self.data['input'])
        self.references = [item['id'] for item in self.data['input']]
        self.references_dict = CiteProcJSON(self.data['input'])
        self.bibliography = CitationStylesBibliography(self.style,
                                                       self.references_dict)
        self.expected = self.data['result'].splitlines()

    @property
    def data(self):
        return self.csl_test.data

    @staticmethod
    def _fix_input(input_data):
        for i, ref in enumerate(input_data):
            if 'id' not in ref:
                ref['id'] = i
            if 'type' not in ref:
                ref['type'] = 'book'

    def execute(self):
        if self.data['citation_items']:
            citations = [
                self.parse_citation(item)
                for item in self.data['citation_items']
            ]
        elif self.data['citations']:
            citations = []
            for cit in self.data['citations']:
                cit = cit[0]
                citation_items = [
                    self.parse_citation_item(cititem)
                    for cititem in cit['citationItems']
                ]
                citation = Citation(citation_items)
                if 'citationID' in cit:
                    citation.key = cit['citationID']
                citation.note_index = cit['properties']['noteIndex']
                citations.append(citation)
        elif self.data['bibentries']:
            citation_items = [
                self.parse_citation_item({'id': entry})
                for entry in self.data['bibentries'][-1]
            ]
            citations = [Citation(citation_items)]
        else:
            citation_items = [
                self.parse_citation_item({'id': ref})
                for ref in self.references
            ]
            citations = [Citation(citation_items)]

        for citation in citations:
            self.bibliography.register(citation)

        if self.style.has_bibliography():
            self.bibliography.sort()

        results = []
        do_nothing = lambda x: None  # callback passed to cite()
        if self.data['mode'] == 'citation':
            if self.data['citations']:
                for i, citation in enumerate(citations):
                    if i == len(citations) - 1:
                        dots_or_other = '>>'
                    else:
                        dots_or_other = '..'
                    results.append(
                        '{}[{}] '.format(dots_or_other, i) +
                        self.bibliography.cite(citation, do_nothing))
            else:
                for citation in citations:
                    results.append(self.bibliography.cite(
                        citation, do_nothing))
        elif self.data['mode'] in ('bibliography', 'bibliography-nosort'):
            results.append(self.bib_prefix)
            for entry in self.bibliography.bibliography():
                text = self.item_prefix + str(entry) + self.item_suffix
                results.append(text)
            results.append(self.bib_suffix)
        return results

    def parse_citation(self, citation_data):
        citation_items = []
        for item in citation_data:
            citation_item = self.parse_citation_item(item)
            citation_items.append(citation_item)
        return Citation(citation_items)

    def parse_citation_item(self, citation_item_data):
        options = {}
        for key, value in citation_item_data.items():
            python_key = key.replace('-', '_')
            if python_key == 'id':
                reference_key = str(value)
                continue
            elif python_key == 'locator':
                try:
                    options['locator'] = Locator(citation_item_data['label'],
                                                 value)
                except KeyError:
                    # some tests don't specify the label
                    options['locator'] = Locator('page', value)
            elif python_key == 'label':
                pass
            else:
                options[python_key] = value
        return CitationItem(reference_key, **options)
Пример #55
0
 def __init__(self, bib_file, csl_file):
     self.bib_file = bib_file
     self.csl_file = csl_file
     self.bibtex = BibTeX(bib_file)
     self.style = CitationStylesStyle(csl_file, validate=False)
     self.bibliography = CitationStylesBibliography(bib_style, bib_source,formatter.plain)
Пример #56
0
# Save it and reopen it, because the documentation doesn't describe a way to load a string
with open('bibtex.bib', 'w') as bibtex_file:
    bibtexparser.dump(bibTeX, bibtex_file)
bib_source = BibTeX('bibtex.bib', encoding='utf-8')

# load a CSL style
fCSL = open('static/csl/mla-isawbib-full.csl', "rb")
fCSL = open('static/csl/mla-isawbib-authorless.csl', "rb")
bib_style = CitationStylesStyle(fCSL, validate=False)

# Create the citeproc-py bibliography, passing it the:
# * CitationStylesStyle,
# * BibliographySource (CiteProcJSON in this case), and
# * a formatter (plain, html, or you can write a custom formatter)

bibliography = CitationStylesBibliography(bib_style, bib_source,
                                          formatter.html)

# Processing citations in a document needs to be done in two passes as for some
# CSL styles, a citation can depend on the order of citations in the
# bibliography and thus on citations following the current one.
# For this reason, we first need to register all citations with the
# CitationStylesBibliography.
for item in bibTeX.entries:
    print(item['ID'])
    citation = Citation([CitationItem(item['ID'])])
    bibliography.register(citation)

print('')
print('Bibliography')
print('------------')
Пример #57
0
def bib(style_path,
        ds,
        return_cites_and_keys = False,
        formatter = "chocolate",
        apa_tweaks = True,
        # The below options are ignored unless apa_tweaks is on.
        always_include_issue = False,
        include_isbn = False,
        url_after_doi = False,
        publisher_website = True,
        abbreviate_given_names = True):

    if isinstance(formatter, str):
        try:             formatter = formatter_from_name[formatter]
        except KeyError: raise ValueError('Unknown formatter "{}"'.format(formatter))        

    style = get_style(style_path, apa_tweaks,
        include_isbn, url_after_doi, abbreviate_given_names)

    ds = deepcopy(ds)
    if apa_tweaks:
    # Distinguish entries that would have identical authors and years
    # by adding suffixes to the years.
        # Group works by author and year.
        #
        # (Actually, we use only an initial subset of authors,
        # the same number that would be included in an inline citation
        # after the first inline citation. This is 2 for 2 authors
        # and 1 otherwise.)
        ay = defaultdict(list)
        for d in ds:
            names = d.get('author') or d.get('editor')
            if len(names) != 2:
                names = [names[0]]
            k = repr(names)  + '/' + str(d['issued']['date-parts'][0][0])
            if not any(d is v for v in ay[k]):
                ay[k].append(d)
        # If any group has more than one element, add suffixes.
        for v in ay.values():
            if len(v) > 1:
                for i, d in enumerate(sorted(v, key = title_sort_key)):
                   d['year_suffix'] = ascii_lowercase[i]
    for d in ds:
        if 'id' not in d:
            d['id'] = str(random())
        for k in list(d.keys()):
            if d[k] is None: del d[k]
        if apa_tweaks:
            # By default, don't include the issue number for
            # journal articles.
            if not always_include_issue and d['type'] == 'article-journal':
                delf(d, 'issue')
            # Use the weird "Retrieved from Dewey, Cheatem, &
            # Howe website: http://example.com" format prescribed
            # for reports.
            if publisher_website and d['type'] == 'report' and 'publisher' in d and 'URL' in d:
                d['URL'] = '{} website: {}'.format(
                    d.pop('publisher'), d['URL'])
            # Add structure words for presentations and include
            # the event place.
            if d['type'] == 'speech' and d['genre'] == 'paper':
                d['event'] = 'meeting of the {}, {}'.format(
                    d.pop('publisher'), d['event-place'])
            if d['type'] == 'speech' and d['genre'] == 'video':
                d['medium'] = 'Video file'
                del d['genre']
            # Format encyclopedia entries like book chapters.
            if d['type'] == 'entry-encyclopedia':
                d['type'] = 'chapter'
            # When abbreviating given names, remove hyphens
            # preceding lowercase letters. Otherwise, weird
            # stuff happens.
            if abbreviate_given_names and 'author' in d:
               for a in d['author']:
                   if 'given' in a:
                       a['given'] = sub(
                           '-(.)',
                           lambda mo:
                               ("" if mo.group(1).islower() else "-") +
                               mo.group(1),
                           a['given'])

    bibliography = CitationStylesBibliography(
        style,
        CiteProcJSON(ds),
        formatter)
    cites = [ Citation([CitationItem(d['id'])]) for d in ds ]
    for c in cites: bibliography.register(c)
    def sort_key_f(item):
        ref = item.reference
        names = [(name['family'].lower(), name['given'][0].lower() if 'given' in name else '')
            for name in ref.get('author') or ref.get('editor')]
        return (names, ref['issued']['year'],
            title_sort_key(ref),
            ref['page']['first'] if 'page' in ref else '')
    if len(ds) > 1:
        # Sort the bibliography
        # bibliography.sort()   # Doesn't appear to handle leading "the"s correctly.
        bibliography.items = sorted(bibliography.items, key = sort_key_f)
        bibliography.keys = [item.key for item in bibliography.items]
    bibl = bibliography.bibliography()

    for i, s in enumerate(bibl):
        s = ''.join(s)
        # Fix spacing and punctuation issues.
        s = s.replace('  ', ' ')
        s = sub(r'([.!?…])\.', r'\1', s)
        if apa_tweaks:
            if formatter is citeproc.formatter.html or formatter is chocolate:
                # Italicize the stuff between a journal name and a volume
                # number.
                s = sub(r'</i>, <i>(\d)', r', \1', s)
                # Remove redundant periods that are separated
                # from the first end-of-sentence mark by an </i>
                # tag.
                s = sub(r'([.!?…]</i>)\.', r'\1', s)
            # If there are two authors and the first is a mononym,
            # remove the comma after it.
            s = sub('^([^.,]+), &', r'\1 &', s)
        bibl[i] = s

    if return_cites_and_keys:
        fcites = [bibliography.cite(c, lambda x: None) for c in cites]
        return (fcites, bibliography.keys, bibl)
    else:
        return bibl