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
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)
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)
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)
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)
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)
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)
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)
formatter.html) ###################################################################################################### ###################################################################################################### ####### docx document object doc = docx.Document() doc.add_heading("References", 0) print("References") for item in bib_source: citation = Citation([CitationItem(item)]) bibliography.register(citation) item_string = bibliography.cite(citation, warn) html_helper = HTMLHelper() bibliography.sort() for item in bibliography.bibliography(): ###################################################################################################### ###################################################################################################### ####### take out extra characters in citation, that are artifacts of the citeproc citation ####### creation process with some of our bib records ####### ####### Also make the editor label plural if there are multiple ("eds.") item = str(item) item = item.replace(", n.d..", "") item = item.replace(',,', ',') item = item.replace('..', '.') item = re.sub(r'([^<]+?and[^<]+?)(ed.)(\s+<i>)', r'\1eds.\3', item) item = item.replace(',.', '.') ######################################################################################################