diff --git a/setup.py b/setup.py index b30eba6..856dbbd 100644 --- a/setup.py +++ b/setup.py @@ -1,22 +1,22 @@ from setuptools import setup from setuptools import find_packages setup( name="texla", - version="0.1.2", + version="0.1.3", author="Davide Valsecchi", author_email="valsecchi.davide94@gmail.com", url="https://github.com/WikiToLearn/texla", license="GPL", packages=find_packages(), install_requires=[ "PyYaml", "requests" ], python_requires='>=3', include_package_data=True, entry_points={ "console_scripts" : [ "texla=texla.scripts.texla:main" ] } ) diff --git a/texla/Parser/Blocks/BibliographyBlock.py b/texla/Parser/Blocks/BibliographyBlock.py index f6a8bc7..9a4d484 100644 --- a/texla/Parser/Blocks/BibliographyBlock.py +++ b/texla/Parser/Blocks/BibliographyBlock.py @@ -1,82 +1,82 @@ import logging from ..Utilities import * from .Block import Block class ThebibliographyBlock(Block): ''' This block represents the Thebibliography environment ''' @staticmethod def parse(parser, tex, parent_block, params): '''We parse the content of the env. Then we analyze the blocks and find which are items and not. The hierarchy of blocks is constructed after the parsing of the content. It's the only way to let the parser handle nested environments. Then, all the blocks are reappended under items blocks and added as children nodes. ''' - options, left_tex = CommandParser.parse_options(tex, [('opt',' {','}')]) + options, left_tex = CommandParser.parse_options(tex, [('opt','{','}')]) block = ThebibliographyBlock(options["opt"], tex, parent_block) #parsing children blocks ch_blocks = parser.parse_instructions(left_tex, parent_block,{}) #now we search for item blocks item_blocks = [] for i,bl in enumerate(ch_blocks): if isinstance(bl,BibitemBlock): item_blocks.append(bl) # Save the bibitem in the thebibliography map block.items[bl.attributes["label"]] = bl #all block until we reach another #item is added to the item block j = i while True: if j+1 < len(ch_blocks): bll = ch_blocks[j+1] if isinstance(bll,BibitemBlock): break #changin parent bll.change_parent_block(bl) #adding block to #item children bl.add_child_block(bll) j+=1 else: break #adding items blocks to children block.add_children_blocks(item_blocks) return block def __init__(self, options, tex, parent_block): super().__init__("thebibliography",tex,parent_block) self.attributes["options"] = options self.items = {} class BibitemBlock(Block): ''' This is a placeholder for a thebibliography environment entry''' @staticmethod def parse (parser, tex, parent_block, params): options, left_tex = CommandParser.parse_options( tex, [('label','{','}')]) label = options['label'] block = BibitemBlock(label, parent_block) #if there's a column in the left text is removed return (block, left_tex.strip()) def __init__(self, label, parent_block): super().__init__('bibitem', label,parent_block) self.attributes['label'] = label def __str__(self): return ''.format( self.block_name, self.id, self.attributes["label"]) parser_hooks = { 'thebibliography': ThebibliographyBlock.parse, 'bibitem': BibitemBlock.parse } diff --git a/texla/Parser/Blocks/CiteBlock.py b/texla/Parser/Blocks/CiteBlock.py index fb262bb..01fe994 100644 --- a/texla/Parser/Blocks/CiteBlock.py +++ b/texla/Parser/Blocks/CiteBlock.py @@ -1,28 +1,24 @@ import logging from ..Utilities import * from .Block import Block class CiteBlock(Block): ''' Block for citation''' @staticmethod def parse (parser, tex, parent_block, params): options, left_tex = CommandParser.parse_options( - tex, [('label','{','}')]) - label = options['label'] - block = CiteBlock(label, parent_block) + tex, [('labels','{','}')]) + labels = [l.strip() for l in options['labels'].split(',')] + block = CiteBlock(labels, parent_block) #if there's a column in the left text is removed return (block, left_tex) - def __init__(self, label, parent_block): - super().__init__('cite', label,parent_block) - self.attributes['label'] = label - - def __str__(self): - return ''.format( - self.block_name, self.id, self.attributes["label"]) + def __init__(self, labels, parent_block): + super().__init__('cite', ",".join(labels), parent_block) + self.attributes['labels'] = labels parser_hooks = { 'cite': CiteBlock.parse } diff --git a/texla/Renderers/plugins/reorder_bibliography.py b/texla/Renderers/plugins/reorder_bibliography.py index a69e125..cfc215d 100644 --- a/texla/Renderers/plugins/reorder_bibliography.py +++ b/texla/Renderers/plugins/reorder_bibliography.py @@ -1,28 +1,33 @@ import re from ..utils import * import logging cit_order = [] -bibliography_items = {} +bib_block = None def save_cite(block): - cit_order.append(block.attributes["label"]) + global cit_order + cit_order += block.attributes["labels"] def save_biblio(block): - global bibliography_items - bibliography_items = block.items - print(bibliography_items) + global bib_block + bib_block = block def print_biblio(): + print(len(cit_order)) with open(configs["output_path"], "w") as f: + opts = bib_block.attributes["options"] + f.write(f"\\begin{{thebibliography}}{{{opts}}}\n") written_items = [] for c in cit_order: if c not in written_items: - f.write(f'\\bibitem{{{c}}}\n{bibliography_items[c].get_content_children()}\n\n') + logging.info(f"Printing bibitem {c}") + f.write(f'\\bibitem{{{c}}}\n{bib_block.items[c].get_content_children()}\n\n') written_items.append(c) + f.write("\\end{thebibliography}") plugin_render_hooks = { 'cite' : { "pre": save_cite }, 'thebibliography' : { "pre": save_biblio } } plugin_lifecycle_hooks = {"end": print_biblio } \ No newline at end of file