From 22ed4c8a5a26953ee9873086583c1d3b0acb6f1f Mon Sep 17 00:00:00 2001 From: Jonathan Grizou Date: Sun, 7 Jul 2013 15:07:20 +0200 Subject: [PATCH] Cleaner/smarter autofill. We also autofill the location of the style and bibfile. We also allow for the user to choose the bibstyle in the config file. --- papers/plugs/texnote/autofill_tools.py | 49 +++++++++++++++----------- papers/plugs/texnote/default_body.tex | 9 +++-- papers/plugs/texnote/texnote.py | 17 ++++++++- 3 files changed, 51 insertions(+), 24 deletions(-) diff --git a/papers/plugs/texnote/autofill_tools.py b/papers/plugs/texnote/autofill_tools.py index 7aa6959..9aad33d 100644 --- a/papers/plugs/texnote/autofill_tools.py +++ b/papers/plugs/texnote/autofill_tools.py @@ -1,9 +1,17 @@ AUTOFILL_TPL = '\\autofill{FIELD}{INFO}' + +def get_autofill_pattern(field): + return AUTOFILL_TPL.replace('FIELD', field) + + def autofill(text, paper): - for field, repl in get_autofill_info(paper): - text = text.replace(find_autofill_pattern(text, field), - autofill_repl(repl, field)) + for field, info in get_autofill_info(paper): + print find_pattern(text, get_autofill_pattern(field)) + print fill_pattern(get_autofill_pattern(field), info) + text = replace_pattern(text, + get_autofill_pattern(field), + info) return text def get_autofill_info(paper): @@ -20,25 +28,12 @@ def get_autofill_info(paper): return info -def get_author_as_str(paper): - persons = paper.bibentry.persons - authors = [] - if 'author' in persons: - for author in persons['author']: - authors.append(format_author(author)) - return concatenate_authors(authors) - - -def autofill_template(field): - return AUTOFILL_TPL.replace('FIELD', field) +def fill_pattern(pattern, info): + return pattern.replace('INFO', info) -def autofill_repl(repl, field): - return autofill_template(field).replace('INFO', repl) - - -def find_autofill_pattern(text, field): - pattern = autofill_template(field).replace('INFO}', '') +def find_pattern(text, pattern): + pattern = pattern.replace('INFO}', '') start = text.find(pattern) after = start + len(pattern) info_length = text[after:].find('}') @@ -46,7 +41,21 @@ def find_autofill_pattern(text, field): return text[start:end] +def replace_pattern(text, pattern, info): + return text.replace(find_pattern(text, pattern), + fill_pattern(pattern, info)) + + ##### ugly replace by proper ##### +def get_author_as_str(paper): + persons = paper.bibentry.persons + authors = [] + if 'author' in persons: + for author in persons['author']: + authors.append(format_author(author)) + return concatenate_authors(authors) + + def format_author(author): first = author.first() middle = author.middle() diff --git a/papers/plugs/texnote/default_body.tex b/papers/plugs/texnote/default_body.tex index 546b6fd..460b0a3 100644 --- a/papers/plugs/texnote/default_body.tex +++ b/papers/plugs/texnote/default_body.tex @@ -3,7 +3,7 @@ %TITLE, AUTHOR, YEAR, ABSTRACT will be automatyically replaced with respect to the associated bibfile thanks to the \autofill{*FIELD*}{} marker. \documentclass{article} %All texnote much share the same style so that we can compile them all together without problem -\usepackage{template/style} +\usepackage{INFO} %You are free to edit the style file using: papers texnote edit_template -S \begin{document} %You can edit the template of this file using: papers texnote edit_template -B @@ -23,7 +23,10 @@ %DO_NOT_MODIFY{ %You can only cite papers added in you repo. %To update the bib file with latest papers info: papers texnote generate_bib -\bibliographystyle{IEEE} -\bibliography{template/bib} +\bibliographystyle{INFO} %default is ieeetr +%You can change the bibliography style in the config file : .papersrc +%[texnote] +%bib_style = plain +\bibliography{INFO} \end{document} %} diff --git a/papers/plugs/texnote/texnote.py b/papers/plugs/texnote/texnote.py index 756cb45..b48b786 100644 --- a/papers/plugs/texnote/texnote.py +++ b/papers/plugs/texnote/texnote.py @@ -13,7 +13,7 @@ from ...plugins import PapersPlugin from ...events import RemoveEvent, RenameEvent, AddEvent from ...commands.helpers import add_references_argument, parse_reference -from .autofill_tools import autofill +from .autofill_tools import autofill, replace_pattern SECTION = 'texnote' @@ -26,6 +26,12 @@ TPL_BIB = os.path.join(TPL_DIR, 'bib.bib') DFT_BODY = os.path.join(os.path.dirname(__file__), 'default_body.tex') DFT_STYLE = os.path.join(os.path.dirname(__file__), 'default_style.sty') +STYLE_PATTERN = '\\usepackage{INFO}' +STYLE_INFO = os.path.splitext(TPL_STYLE)[0].replace(DIR, '')[1:] +BIB_PATTERN = '\\bibliography{INFO}' +BIB_INFO = os.path.splitext(TPL_BIB)[0].replace(DIR, '')[1:] +BIBSTYLE_PATTERN = '\\bibliographystyle{INFO}' +DFT_BIBSTYLE_INFO = 'ieeetr' class TexnotePlugin(PapersPlugin): @@ -48,6 +54,8 @@ class TexnotePlugin(PapersPlugin): shutil.copy(DFT_BODY, TPL_BODY) if not files.check_file(TPL_STYLE): shutil.copy(DFT_STYLE, TPL_STYLE) + if not files.check_file(TPL_BIB): + self.generate_bib() def parser(self, subparsers): parser = subparsers.add_parser(self.name, help='edit advance note in latex') @@ -88,6 +96,10 @@ class TexnotePlugin(PapersPlugin): if not files.check_file(self._texfile(citekey)): shutil.copy(TPL_BODY, self._texfile(citekey)) + def get_bib_style(self): + default = DFT_BIBSTYLE_INFO + return config(SECTION).get('bib_style', default) + def _autofill_texfile(self, citekey): with open(self._texfile(citekey)) as f: text = f.read() @@ -95,6 +107,9 @@ class TexnotePlugin(PapersPlugin): if citekey in rp: paper = rp.get_paper(citekey) text = autofill(text, paper) + text = replace_pattern(text, STYLE_PATTERN, STYLE_INFO) + text = replace_pattern(text, BIB_PATTERN, BIB_INFO) + text = replace_pattern(text, BIBSTYLE_PATTERN, self.get_bib_style()) with open(self._texfile(citekey), "w") as f: f.write(text)