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.
main
Jonathan Grizou 12 years ago
parent 15870ecffe
commit 22ed4c8a5a

@ -1,9 +1,17 @@
AUTOFILL_TPL = '\\autofill{FIELD}{INFO}' AUTOFILL_TPL = '\\autofill{FIELD}{INFO}'
def get_autofill_pattern(field):
return AUTOFILL_TPL.replace('FIELD', field)
def autofill(text, paper): def autofill(text, paper):
for field, repl in get_autofill_info(paper): for field, info in get_autofill_info(paper):
text = text.replace(find_autofill_pattern(text, field), print find_pattern(text, get_autofill_pattern(field))
autofill_repl(repl, field)) print fill_pattern(get_autofill_pattern(field), info)
text = replace_pattern(text,
get_autofill_pattern(field),
info)
return text return text
def get_autofill_info(paper): def get_autofill_info(paper):
@ -20,25 +28,12 @@ def get_autofill_info(paper):
return info return info
def get_author_as_str(paper): def fill_pattern(pattern, info):
persons = paper.bibentry.persons return pattern.replace('INFO', info)
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 autofill_repl(repl, field): def find_pattern(text, pattern):
return autofill_template(field).replace('INFO', repl) pattern = pattern.replace('INFO}', '')
def find_autofill_pattern(text, field):
pattern = autofill_template(field).replace('INFO}', '')
start = text.find(pattern) start = text.find(pattern)
after = start + len(pattern) after = start + len(pattern)
info_length = text[after:].find('}') info_length = text[after:].find('}')
@ -46,7 +41,21 @@ def find_autofill_pattern(text, field):
return text[start:end] 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 ##### ##### 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): def format_author(author):
first = author.first() first = author.first()
middle = author.middle() middle = author.middle()

@ -3,7 +3,7 @@
%TITLE, AUTHOR, YEAR, ABSTRACT will be automatyically replaced with respect to the associated bibfile thanks to the \autofill{*FIELD*}{} marker. %TITLE, AUTHOR, YEAR, ABSTRACT will be automatyically replaced with respect to the associated bibfile thanks to the \autofill{*FIELD*}{} marker.
\documentclass{article} \documentclass{article}
%All texnote much share the same style so that we can compile them all together without problem %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 %You are free to edit the style file using: papers texnote edit_template -S
\begin{document} \begin{document}
%You can edit the template of this file using: papers texnote edit_template -B %You can edit the template of this file using: papers texnote edit_template -B
@ -23,7 +23,10 @@
%DO_NOT_MODIFY{ %DO_NOT_MODIFY{
%You can only cite papers added in you repo. %You can only cite papers added in you repo.
%To update the bib file with latest papers info: papers texnote generate_bib %To update the bib file with latest papers info: papers texnote generate_bib
\bibliographystyle{IEEE} \bibliographystyle{INFO} %default is ieeetr
\bibliography{template/bib} %You can change the bibliography style in the config file : .papersrc
%[texnote]
%bib_style = plain
\bibliography{INFO}
\end{document} \end{document}
%} %}

@ -13,7 +13,7 @@ from ...plugins import PapersPlugin
from ...events import RemoveEvent, RenameEvent, AddEvent from ...events import RemoveEvent, RenameEvent, AddEvent
from ...commands.helpers import add_references_argument, parse_reference from ...commands.helpers import add_references_argument, parse_reference
from .autofill_tools import autofill from .autofill_tools import autofill, replace_pattern
SECTION = 'texnote' 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_BODY = os.path.join(os.path.dirname(__file__), 'default_body.tex')
DFT_STYLE = os.path.join(os.path.dirname(__file__), 'default_style.sty') 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): class TexnotePlugin(PapersPlugin):
@ -48,6 +54,8 @@ class TexnotePlugin(PapersPlugin):
shutil.copy(DFT_BODY, TPL_BODY) shutil.copy(DFT_BODY, TPL_BODY)
if not files.check_file(TPL_STYLE): if not files.check_file(TPL_STYLE):
shutil.copy(DFT_STYLE, TPL_STYLE) shutil.copy(DFT_STYLE, TPL_STYLE)
if not files.check_file(TPL_BIB):
self.generate_bib()
def parser(self, subparsers): def parser(self, subparsers):
parser = subparsers.add_parser(self.name, help='edit advance note in latex') 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)): if not files.check_file(self._texfile(citekey)):
shutil.copy(TPL_BODY, 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): def _autofill_texfile(self, citekey):
with open(self._texfile(citekey)) as f: with open(self._texfile(citekey)) as f:
text = f.read() text = f.read()
@ -95,6 +107,9 @@ class TexnotePlugin(PapersPlugin):
if citekey in rp: if citekey in rp:
paper = rp.get_paper(citekey) paper = rp.get_paper(citekey)
text = autofill(text, paper) 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: with open(self._texfile(citekey), "w") as f:
f.write(text) f.write(text)

Loading…
Cancel
Save