Texnote usable.
Added a edit_file function in files.
This commit is contained in:
parent
1f91712b31
commit
76d1ef3931
@ -206,3 +206,17 @@ def editor_input(editor, initial="", suffix=None):
|
|||||||
content = temp_file.read()
|
content = temp_file.read()
|
||||||
os.remove(tfile_name)
|
os.remove(tfile_name)
|
||||||
return content
|
return content
|
||||||
|
|
||||||
|
|
||||||
|
def edit_file(editor, path_to_file, temporary=True):
|
||||||
|
if temporary:
|
||||||
|
check_file(path_to_file, fail=True)
|
||||||
|
with open(path_to_file) as f:
|
||||||
|
content = f.read()
|
||||||
|
content = editor_input(editor, content)
|
||||||
|
with open(path_to_file, 'w') as f:
|
||||||
|
f.write(content)
|
||||||
|
else:
|
||||||
|
cmd = editor.split() # this enable editor command with option, e.g. gvim -f
|
||||||
|
cmd.append(path_to_file)
|
||||||
|
subprocess.call(cmd)
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
\documentclass{article}
|
|
||||||
|
|
||||||
\usepackage{amsmath}
|
\usepackage{amsmath}
|
||||||
\usepackage{amsfonts}
|
\usepackage{amsfonts}
|
||||||
\usepackage{amssymb}
|
\usepackage{amssymb}
|
||||||
@ -16,20 +14,3 @@
|
|||||||
|
|
||||||
\usepackage{caption}
|
\usepackage{caption}
|
||||||
\usepackage{subcaption}
|
\usepackage{subcaption}
|
||||||
|
|
||||||
\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}
|
|
||||||
|
@ -1,21 +1,6 @@
|
|||||||
\documentclass{article}
|
\documentclass{article}
|
||||||
|
|
||||||
\usepackage{amsmath}
|
\usepackage{style}
|
||||||
\usepackage{amsfonts}
|
|
||||||
\usepackage{amssymb}
|
|
||||||
|
|
||||||
\usepackage{algorithm}
|
|
||||||
\usepackage{algorithmic}
|
|
||||||
|
|
||||||
\usepackage{graphics}
|
|
||||||
\usepackage{graphicx}
|
|
||||||
|
|
||||||
\usepackage{hyperref}
|
|
||||||
\usepackage{verbatim}
|
|
||||||
\usepackage{url}
|
|
||||||
|
|
||||||
\usepackage{caption}
|
|
||||||
\usepackage{subcaption}
|
|
||||||
|
|
||||||
\begin{document}
|
\begin{document}
|
||||||
|
|
||||||
@ -26,7 +11,7 @@
|
|||||||
\end{center}
|
\end{center}
|
||||||
|
|
||||||
\begin{abstract}
|
\begin{abstract}
|
||||||
ABSTRACT
|
ABSTRACT
|
||||||
\end{abstract}
|
\end{abstract}
|
||||||
|
|
||||||
\section{Notes}
|
\section{Notes}
|
||||||
|
@ -64,76 +64,84 @@ class TexnotePlugin(PapersPlugin):
|
|||||||
del args.texcmd
|
del args.texcmd
|
||||||
self.texcmds[texcmd](**vars(args))
|
self.texcmds[texcmd](**vars(args))
|
||||||
|
|
||||||
def remove(self, ui, reference):
|
def _texfile(self, citekey):
|
||||||
|
return os.path.join(TEXNOTE_DIR, citekey + '.tex')
|
||||||
|
|
||||||
|
def _ensure_texfile(self, citekey):
|
||||||
|
if not files.check_file(self._texfile(citekey)):
|
||||||
|
shutil.copy(TEXNOTE_DEFAULT_TEMPLATE, 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())
|
rp = repo.Repository(config())
|
||||||
key = parse_reference(ui, rp, reference)
|
paper = rp.get_paper(citekey)
|
||||||
print('Should remove {}'.format(key))
|
fields = paper.bibentry.fields
|
||||||
|
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:
|
||||||
|
f.write(text)
|
||||||
|
|
||||||
|
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"""
|
||||||
|
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)
|
||||||
|
|
||||||
def edit(self, ui, reference, view=None):
|
def edit(self, ui, reference, view=None):
|
||||||
print('Should edit {}'.format(reference))
|
|
||||||
if view is not None:
|
if view is not None:
|
||||||
subprocess.Popen(['papers', 'open', reference])
|
subprocess.Popen(['papers', 'open', reference])
|
||||||
|
|
||||||
#open_texnote(ui, reference)
|
rp = repo.Repository(config())
|
||||||
|
citekey = parse_reference(ui, rp, reference)
|
||||||
|
files.edit_file(self.get_edit_cmd(), self.get_texfile(citekey), temporary=False)
|
||||||
|
|
||||||
def edit_style(self):
|
def edit_style(self, ui):
|
||||||
pass
|
files.edit_file(self.get_edit_cmd(), TEXNOTE_STYLE)
|
||||||
|
|
||||||
def edit_template(self):
|
def edit_template(self, ui):
|
||||||
pass
|
files.edit_file(self.get_edit_cmd(), TEXNOTE_TEMPLATE)
|
||||||
|
|
||||||
def toto(self):
|
def remove(self, ui, reference):
|
||||||
print "toto"
|
rp = repo.Repository(config())
|
||||||
|
citekey = parse_reference(ui, rp, reference)
|
||||||
#@RemoveEvent.listen()
|
try:
|
||||||
def testEvent(self, rmevent):
|
os.remove(self._texfile(citekey))
|
||||||
print "testEvent"
|
except OSError:
|
||||||
|
pass # For some reason, the texnote file didn't exist
|
||||||
|
|
||||||
|
|
||||||
@RemoveEvent.listen()
|
@RemoveEvent.listen()
|
||||||
def remove(rmevent):
|
def remove(rmevent):
|
||||||
texplug = TexnotePlugin.get_instance()
|
texplug = TexnotePlugin.get_instance()
|
||||||
texplug.remove(rmevent.ui, rmevent.citekey)
|
texplug.remove(rmevent.ui, rmevent.citekey)
|
||||||
# HACK : transfer repo via RemoveEvent, do not recreate one
|
|
||||||
#rp = repo.Repository(config())
|
|
||||||
#paper = rp.get_paper(parse_reference(rmevent.ui, rp, rmevent.citekey))
|
|
||||||
#if 'texnote' in paper.metadata:
|
|
||||||
# try:
|
|
||||||
# os.remove(paper.metadata['texnote'])
|
|
||||||
# except OSError:
|
|
||||||
# pass # For some reason, the texnote file didn't exist
|
|
||||||
# paper.metadata.pop('texnote')
|
|
||||||
# metapath = rp.path_to_paper_file(paper.citekey, 'meta')
|
|
||||||
# files.save_meta(paper.metadata, metapath)
|
|
||||||
|
|
||||||
|
|
||||||
def open_texnote(ui, ref):
|
|
||||||
# HACK : transfer repo via arguments, do not recreate one
|
|
||||||
rp = repo.Repository(config())
|
|
||||||
paper = rp.get_paper(parse_reference(ui, rp, ref))
|
|
||||||
|
|
||||||
#ugly to recode like for the doc field
|
|
||||||
if not 'texnote' in paper.metadata:
|
|
||||||
texnote_dir = os.path.join(rp.papersdir, TEXNOTE_DIR)
|
|
||||||
# if folder does not exist create it, this should be relative
|
|
||||||
if not os.path.exists(texnote_dir):
|
|
||||||
os.mkdir(texnote_dir)
|
|
||||||
texnote_path = os.path.join(texnote_dir, paper.citekey + '.tex')
|
|
||||||
paper.metadata['texnote'] = files.clean_path(texnote_path)
|
|
||||||
# save path in metadata
|
|
||||||
metapath = rp.path_to_paper_file(paper.citekey, 'meta')
|
|
||||||
files.save_meta(paper.metadata, metapath)
|
|
||||||
|
|
||||||
texnote_path = paper.metadata['texnote']
|
|
||||||
# test if doc exist else copy the sample one
|
|
||||||
if not files.check_file(texnote_path):
|
|
||||||
shutil.copyfile(TEXNOTE_SAMPLE_FILE, texnote_path)
|
|
||||||
#should autofill at every opening or not ? usefull if bib changes but the filling should be improved
|
|
||||||
autofill_texnote(texnote_path, paper.bibentry)
|
|
||||||
|
|
||||||
#open the file using the config editor
|
|
||||||
edit_cmd = config(TEXNOTE_SECTION).get('edit_cmd', config().edit_cmd)
|
|
||||||
subprocess.Popen([edit_cmd, texnote_path])
|
|
||||||
|
|
||||||
|
|
||||||
##### ugly replace by proper #####
|
##### ugly replace by proper #####
|
||||||
@ -162,38 +170,3 @@ def concatenate_authors(authors):
|
|||||||
concatenated += authors[a]
|
concatenated += authors[a]
|
||||||
return concatenated
|
return concatenated
|
||||||
#####
|
#####
|
||||||
|
|
||||||
|
|
||||||
def autofill_texnote(texnote_path, bibentry):
|
|
||||||
# read file
|
|
||||||
f = open(texnote_path, "r")
|
|
||||||
text = f.read()
|
|
||||||
f.close()
|
|
||||||
# modify with bib info
|
|
||||||
#print bibentry
|
|
||||||
fields = bibentry.fields
|
|
||||||
persons = 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)
|
|
||||||
|
|
||||||
# write file
|
|
||||||
f = open(texnote_path, "w")
|
|
||||||
f.write(text)
|
|
||||||
f.close()
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user