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.
This commit is contained in:
parent
15870ecffe
commit
22ed4c8a5a
@ -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,6 +28,25 @@ def get_autofill_info(paper):
|
||||
return info
|
||||
|
||||
|
||||
def fill_pattern(pattern, info):
|
||||
return pattern.replace('INFO', info)
|
||||
|
||||
|
||||
def find_pattern(text, pattern):
|
||||
pattern = pattern.replace('INFO}', '')
|
||||
start = text.find(pattern)
|
||||
after = start + len(pattern)
|
||||
info_length = text[after:].find('}')
|
||||
end = start + len(pattern) + info_length + 1
|
||||
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 = []
|
||||
@ -29,24 +56,6 @@ def get_author_as_str(paper):
|
||||
return concatenate_authors(authors)
|
||||
|
||||
|
||||
def autofill_template(field):
|
||||
return AUTOFILL_TPL.replace('FIELD', field)
|
||||
|
||||
|
||||
def autofill_repl(repl, field):
|
||||
return autofill_template(field).replace('INFO', repl)
|
||||
|
||||
|
||||
def find_autofill_pattern(text, field):
|
||||
pattern = autofill_template(field).replace('INFO}', '')
|
||||
start = text.find(pattern)
|
||||
after = start + len(pattern)
|
||||
info_length = text[after:].find('}')
|
||||
end = start + len(pattern) + info_length + 1
|
||||
return text[start:end]
|
||||
|
||||
|
||||
##### ugly replace by proper #####
|
||||
def format_author(author):
|
||||
first = author.first()
|
||||
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.
|
||||
\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}
|
||||
%}
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user