Texnote robust. Should implement test now.

main
jgrizou 12 years ago
parent 248055ded9
commit 4f4a58d81c

@ -0,0 +1,26 @@
%DO_NOT_MODIFY{
%This file is the starting point for all notes
%TITLE, AUTHOR, YEAR, ABSTRACT will be automatyically replaced with respect to the associated bibfile
%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}
\usepackage{template/style}
\begin{document}
\begin{center}
\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
%\section{Notes}
% Write your notes here
%DO_NOT_MODIFY{
\end{document}
%}

@ -1,20 +0,0 @@
\documentclass{article}
\usepackage{style}
\begin{document}
\begin{center}
\Large{\textbf{TITLE}} \\ [0.2cm]
\small{\textsc{AUTHOR}} \\ [0.2cm]
\normalsize{\textsc{YEAR}} \\ [1cm]
\end{center}
\begin{abstract}
ABSTRACT
\end{abstract}
\section{Notes}
Write your notes here.
\end{document}

@ -11,34 +11,36 @@ from ...commands.helpers import add_references_argument, parse_reference
from ...events import RemoveEvent, RenameEvent, AddEvent
TEXNOTE_SECTION = 'texnote'
TEXNOTE_DIR = os.path.join(config().papers_dir, 'texnote')
TEXNOTE_TEMPLATE = os.path.join(TEXNOTE_DIR, 'template.tex')
TEXNOTE_STYLE = os.path.join(TEXNOTE_DIR, 'style.sty')
SECTION = 'texnote'
DIR = os.path.join(config().papers_dir, 'texnote')
TPL_DIR = os.path.join(DIR, 'template')
TPL_BODY = os.path.join(TPL_DIR, 'body.tex')
TPL_STYLE = os.path.join(TPL_DIR, 'style.sty')
TEXNOTE_DEFAULT_TEMPLATE = os.path.join(os.path.dirname(__file__), 'template.tex')
TEXNOTE_DEFAULT_STYLE = os.path.join(os.path.dirname(__file__), 'style.sty')
DFT_BODY = os.path.join(os.path.dirname(__file__), 'default_body.tex')
DFT_STYLE = os.path.join(os.path.dirname(__file__), 'default_style.sty')
class TexnotePlugin(PapersPlugin):
def __init__(self):
self.name = TEXNOTE_SECTION
self.name = SECTION
self.texcmds = collections.OrderedDict([
('remove', self.remove),
('edit', self.edit),
('edit_style', self.edit_style),
('edit_template', self.edit_template),
])
def _ensure_init(self):
if not files.check_directory(TEXNOTE_DIR):
os.mkdir(TEXNOTE_DIR)
if not files.check_file(TEXNOTE_TEMPLATE):
shutil.copy(TEXNOTE_DEFAULT_TEMPLATE, TEXNOTE_TEMPLATE)
if not files.check_file(TEXNOTE_STYLE):
shutil.copy(TEXNOTE_DEFAULT_STYLE, TEXNOTE_STYLE)
if not files.check_directory(DIR):
os.mkdir(DIR)
if not files.check_directory(TPL_DIR):
os.mkdir(TPL_DIR)
if not files.check_file(TPL_BODY):
shutil.copy(DFT_BODY, TPL_BODY)
if not files.check_file(TPL_STYLE):
shutil.copy(DFT_STYLE, TPL_STYLE)
def parser(self, subparsers):
parser = subparsers.add_parser(self.name, help='edit advance note in latex')
@ -53,14 +55,16 @@ class TexnotePlugin(PapersPlugin):
p.add_argument('-w', '--with', dest='with_command', default=None,
help='command to use to open the file')
add_references_argument(p, single=True)
# edit_style
p = sub.add_parser('edit_style', help='edit the latex style used by texnote')
p.add_argument('-w', '--with', dest='with_command', default=None,
help='command to use to open the file')
#edit_template
p = sub.add_parser('edit_template', help='edit the latex template used by texnote')
p.add_argument('-w', '--with', dest='with_command', default=None,
help='command to use to open the file')
p.add_argument('-B', '--body', action='store_true',
help='edit the main body', default=None)
p.add_argument('-S', '--style', action='store_true',
help='open the style', default=None)
p.add_argument('-H', '--header', action='store_true',
help='open the header', default=None)
return parser
def command(self, args):
@ -71,20 +75,18 @@ class TexnotePlugin(PapersPlugin):
self.texcmds[texcmd](**vars(args))
def _texfile(self, citekey):
return os.path.join(TEXNOTE_DIR, citekey + '.tex')
return os.path.join(DIR, citekey + '.tex')
def _ensure_texfile(self, citekey):
if not files.check_file(self._texfile(citekey)):
shutil.copy(TEXNOTE_TEMPLATE, self._texfile(citekey))
shutil.copy(TPL_BODY, self._texfile(citekey))
def _autofill_texfile(self, citekey):
self._ensure_texfile(citekey)
with open(self._texfile(citekey)) as f:
text = f.read()
# modify with bib info
rp = repo.Repository(config())
if citekey in rp:
paper = rp.get_paper(citekey)
fields = paper.bibentry.fields
persons = paper.bibentry.persons
@ -113,13 +115,13 @@ class TexnotePlugin(PapersPlugin):
def get_texfile(self, citekey):
""" This function returns the name of the texfile and
ensure it exist and it is filled with info from the bibfile"""
ensure it exist and it is filled with info from the bibfile if possible"""
self._autofill_texfile(citekey)
return self._texfile(citekey)
def get_edit_cmd(self):
default = config().edit_cmd
return config(TEXNOTE_SECTION).get('edit_cmd', default)
return config(SECTION).get('edit_cmd', default)
def edit(self, ui, reference, view=None, with_command=None):
if view is not None:
@ -131,33 +133,26 @@ class TexnotePlugin(PapersPlugin):
citekey = parse_reference(ui, rp, reference)
files.edit_file(with_command, self.get_texfile(citekey), temporary=False)
def edit_style(self, ui, with_command=None):
def edit_template(self, ui, body=None, style=None, header=None, with_command=None):
if with_command is None:
with_command = self.get_edit_cmd()
files.edit_file(with_command, TEXNOTE_STYLE, temporary=False)
def edit_template(self, ui, with_command=None):
if with_command is None:
with_command = self.get_edit_cmd()
files.edit_file(with_command, TEXNOTE_TEMPLATE, temporary=False)
if body is not None:
files.edit_file(with_command, TPL_BODY, temporary=False)
if style is not None:
files.edit_file(with_command, TPL_STYLE, temporary=False)
def create(self, citekey):
self._autofill_texfile(citekey)
def remove(self, citekey):
try:
os.remove(self._texfile(citekey))
except OSError:
pass # For some reason, the texnote file didn't exist
def remove(self, reference, ui=None):
citekey = reference
if ui is not None:
rp = repo.Repository(config())
citekey = parse_reference(ui, rp, reference)
os.remove(self.get_texfile(citekey))
def rename(self, old_citekey, new_citekey, overwrite=False):
if files.check_file(self._texfile(old_citekey)):
if not files.check_file(self._texfile(new_citekey)) or overwrite:
shutil.move(self._texfile(old_citekey), self._texfile(new_citekey))
else:
raise ValueError('citekey {} already exist'.format(new_citekey))
else:
raise ValueError('citekey {} does not exist'.format(old_citekey))
shutil.move(self.get_texfile(old_citekey), self.get_texfile(new_citekey))
@AddEvent.listen()

@ -100,8 +100,11 @@ class Repository(object):
def get_paper(self, citekey):
"""Load a paper by its citekey from disk, if necessary."""
if citekey in self.citekeys:
return PaperInRepo.load(self, self._bibfile(citekey),
self._metafile(citekey))
else:
raise InvalidReference
def _add_citekey(self, citekey):
if citekey not in self.citekeys:
@ -162,6 +165,7 @@ class Repository(object):
self._write_paper(paper)
else:
self._add_paper(paper, overwrite=overwrite) # This checks for collisions
# We do not want to send the RemoveEvent, associated documents should be moved
self._remove_paper(old_citekey, remove_doc=False)
self._move_doc(old_citekey, paper)
RenameEvent(paper, old_citekey).send()

Loading…
Cancel
Save