Autofill more proper. Use the \autofill{FIELD}{} in body.tex to enable autofill. See the \autofill command in style.sty.
This commit is contained in:
parent
b233c1a0b2
commit
15870ecffe
74
papers/plugs/texnote/autofill_tools.py
Normal file
74
papers/plugs/texnote/autofill_tools.py
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
AUTOFILL_TPL = '\\autofill{FIELD}{INFO}'
|
||||||
|
|
||||||
|
def autofill(text, paper):
|
||||||
|
for field, repl in get_autofill_info(paper):
|
||||||
|
text = text.replace(find_autofill_pattern(text, field),
|
||||||
|
autofill_repl(repl, field))
|
||||||
|
return text
|
||||||
|
|
||||||
|
def get_autofill_info(paper):
|
||||||
|
fields = paper.bibentry.fields
|
||||||
|
|
||||||
|
info = []
|
||||||
|
if 'year' in fields:
|
||||||
|
info.append(('YEAR', fields['year']))
|
||||||
|
if 'title' in fields:
|
||||||
|
info.append(('TITLE', fields['title']))
|
||||||
|
if 'abstract' in fields:
|
||||||
|
info.append(('ABSTRACT', fields['abstract']))
|
||||||
|
info.append(('AUTHOR', get_author_as_str(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 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()
|
||||||
|
last = author.last()
|
||||||
|
formatted = ''
|
||||||
|
if first:
|
||||||
|
formatted += first[0]
|
||||||
|
if middle:
|
||||||
|
formatted += ' ' + middle[0] + '.'
|
||||||
|
if last:
|
||||||
|
formatted += ' ' + last[0]
|
||||||
|
return formatted
|
||||||
|
|
||||||
|
|
||||||
|
def concatenate_authors(authors):
|
||||||
|
concatenated = ''
|
||||||
|
for a in range(len(authors)):
|
||||||
|
if len(authors) > 1 and a > 0:
|
||||||
|
if a == len(authors) - 1:
|
||||||
|
concatenated += ' and '
|
||||||
|
else:
|
||||||
|
concatenated += ', '
|
||||||
|
concatenated += authors[a]
|
||||||
|
return concatenated
|
||||||
|
#####
|
@ -1,26 +1,29 @@
|
|||||||
%DO_NOT_MODIFY{
|
%DO_NOT_MODIFY{
|
||||||
%This file is the starting point for all notes
|
%This file is the starting point for all notes
|
||||||
%TITLE, AUTHOR, YEAR, ABSTRACT will be automatyically replaced with respect to the associated bibfile
|
%TITLE, AUTHOR, YEAR, ABSTRACT will be automatyically replaced with respect to the associated bibfile thanks to the \autofill{*FIELD*}{} marker.
|
||||||
%All texnote much share the same style so that we can compile them all together without problem
|
|
||||||
%You can edit the style file using: papers texnote edit_template -S
|
|
||||||
\documentclass{article}
|
\documentclass{article}
|
||||||
|
%All texnote much share the same style so that we can compile them all together without problem
|
||||||
\usepackage{template/style}
|
\usepackage{template/style}
|
||||||
|
%You are free to edit the style file using: papers texnote edit_template -S
|
||||||
\begin{document}
|
\begin{document}
|
||||||
\begin{center}
|
%You can edit the template of this file using: papers texnote edit_template -B
|
||||||
\Large{\textbf{TITLE}} \\ [0.2cm]
|
|
||||||
\small{\textsc{AUTHOR}} \\ [0.2cm]
|
|
||||||
\normalsize{\textsc{YEAR}} \\ [1cm]
|
|
||||||
\end{center}
|
|
||||||
\begin{abstract}
|
|
||||||
ABSTRACT
|
|
||||||
\end{abstract}
|
|
||||||
%You can edit the following of this file using: papers texnote edit_template -B
|
|
||||||
%}
|
%}
|
||||||
|
|
||||||
%WRITE YOUR NOTES FROM HERE
|
\begin{center}
|
||||||
%\section{Notes}
|
\Large{\textbf{\autofill{TITLE}{The title}}} \\ [0.2cm]
|
||||||
% Write your notes here
|
\small{\textsc{\autofill{AUTHOR}{The authors}}} \\ [0.2cm]
|
||||||
|
\normalsize{\textsc{\autofill{YEAR}{The year}}} \\ [1cm]
|
||||||
|
\end{center}
|
||||||
|
|
||||||
|
\begin{abstract}
|
||||||
|
\autofill{ABSTRACT}{The abstract}
|
||||||
|
\end{abstract}
|
||||||
|
|
||||||
|
|
||||||
%DO_NOT_MODIFY{
|
%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}
|
||||||
\end{document}
|
\end{document}
|
||||||
%}
|
%}
|
||||||
|
@ -1,16 +1,4 @@
|
|||||||
\usepackage{amsmath}
|
%DO_NOT_MODIFY{
|
||||||
\usepackage{amsfonts}
|
%Dummy command used as marker for the autofill
|
||||||
\usepackage{amssymb}
|
\newcommand{\autofill}[2]{#2}
|
||||||
|
%}
|
||||||
\usepackage{algorithm}
|
|
||||||
\usepackage{algorithmic}
|
|
||||||
|
|
||||||
\usepackage{graphics}
|
|
||||||
\usepackage{graphicx}
|
|
||||||
|
|
||||||
\usepackage{hyperref}
|
|
||||||
\usepackage{verbatim}
|
|
||||||
\usepackage{url}
|
|
||||||
|
|
||||||
\usepackage{caption}
|
|
||||||
\usepackage{subcaption}
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import collections
|
import collections
|
||||||
@ -12,6 +13,8 @@ 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
|
||||||
|
|
||||||
|
|
||||||
SECTION = 'texnote'
|
SECTION = 'texnote'
|
||||||
DIR = os.path.join(config().papers_dir, 'texnote')
|
DIR = os.path.join(config().papers_dir, 'texnote')
|
||||||
@ -86,40 +89,20 @@ class TexnotePlugin(PapersPlugin):
|
|||||||
shutil.copy(TPL_BODY, self._texfile(citekey))
|
shutil.copy(TPL_BODY, self._texfile(citekey))
|
||||||
|
|
||||||
def _autofill_texfile(self, citekey):
|
def _autofill_texfile(self, citekey):
|
||||||
self._ensure_texfile(citekey)
|
|
||||||
with open(self._texfile(citekey)) as f:
|
with open(self._texfile(citekey)) as f:
|
||||||
text = f.read()
|
text = f.read()
|
||||||
rp = repo.Repository(config())
|
rp = repo.Repository(config())
|
||||||
if citekey in rp:
|
if citekey in rp:
|
||||||
paper = rp.get_paper(citekey)
|
paper = rp.get_paper(citekey)
|
||||||
fields = paper.bibentry.fields
|
text = autofill(text, paper)
|
||||||
persons = paper.bibentry.persons
|
|
||||||
|
|
||||||
if 'title' in fields:
|
|
||||||
title_str = fields['title']
|
|
||||||
text = text.replace("TITLE", title_str)
|
|
||||||
|
|
||||||
if 'year' in fields:
|
|
||||||
year_str = fields['year']
|
|
||||||
text = text.replace("YEAR", year_str)
|
|
||||||
|
|
||||||
if 'abstract' in fields:
|
|
||||||
abstract_str = fields['abstract']
|
|
||||||
text = text.replace("ABSTRACT", abstract_str)
|
|
||||||
|
|
||||||
if 'author' in persons:
|
|
||||||
authors = []
|
|
||||||
for author in persons['author']:
|
|
||||||
authors.append(format_author(author))
|
|
||||||
author_str = concatenate_authors(authors)
|
|
||||||
text = text.replace("AUTHOR", author_str)
|
|
||||||
|
|
||||||
with open(self._texfile(citekey), "w") as f:
|
with open(self._texfile(citekey), "w") as f:
|
||||||
f.write(text)
|
f.write(text)
|
||||||
|
|
||||||
def get_texfile(self, citekey):
|
def get_texfile(self, citekey, autofill=False):
|
||||||
""" This function returns the name of the texfile and
|
""" This function returns the name of the texfile and
|
||||||
ensure it exist and it is filled with info from the bibfile if possible"""
|
ensure it exist and it is filled with info from the bibfile if possible"""
|
||||||
|
self._ensure_texfile(citekey)
|
||||||
|
if autofill:
|
||||||
self._autofill_texfile(citekey)
|
self._autofill_texfile(citekey)
|
||||||
return self._texfile(citekey)
|
return self._texfile(citekey)
|
||||||
|
|
||||||
@ -135,7 +118,9 @@ class TexnotePlugin(PapersPlugin):
|
|||||||
|
|
||||||
rp = repo.Repository(config())
|
rp = repo.Repository(config())
|
||||||
citekey = parse_reference(rp, reference)
|
citekey = parse_reference(rp, reference)
|
||||||
files.edit_file(with_command, self.get_texfile(citekey), temporary=False)
|
files.edit_file(with_command,
|
||||||
|
self.get_texfile(citekey, autofill=True),
|
||||||
|
temporary=False)
|
||||||
|
|
||||||
def edit_template(self, body=None, style=None, with_command=None):
|
def edit_template(self, body=None, style=None, with_command=None):
|
||||||
if with_command is None:
|
if with_command is None:
|
||||||
@ -179,31 +164,3 @@ def rename(renamevent):
|
|||||||
texplug.rename(renamevent.old_citekey,
|
texplug.rename(renamevent.old_citekey,
|
||||||
renamevent.paper.citekey,
|
renamevent.paper.citekey,
|
||||||
overwrite=True)
|
overwrite=True)
|
||||||
|
|
||||||
|
|
||||||
##### ugly replace by proper #####
|
|
||||||
def format_author(author):
|
|
||||||
first = author.first()
|
|
||||||
middle = author.middle()
|
|
||||||
last = author.last()
|
|
||||||
formatted = ''
|
|
||||||
if first:
|
|
||||||
formatted += first[0]
|
|
||||||
if middle:
|
|
||||||
formatted += ' ' + middle[0] + '.'
|
|
||||||
if last:
|
|
||||||
formatted += ' ' + last[0]
|
|
||||||
return formatted
|
|
||||||
|
|
||||||
|
|
||||||
def concatenate_authors(authors):
|
|
||||||
concatenated = ''
|
|
||||||
for a in range(len(authors)):
|
|
||||||
if len(authors) > 1 and a > 0:
|
|
||||||
if a == len(authors) - 1:
|
|
||||||
concatenated += 'and '
|
|
||||||
else:
|
|
||||||
concatenated += ', '
|
|
||||||
concatenated += authors[a]
|
|
||||||
return concatenated
|
|
||||||
#####
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user