diff --git a/papers/files.py b/papers/files.py index 89aaba9..8a90632 100644 --- a/papers/files.py +++ b/papers/files.py @@ -206,3 +206,17 @@ def editor_input(editor, initial="", suffix=None): content = temp_file.read() os.remove(tfile_name) 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) diff --git a/papers/plugs/texnote/style.sty b/papers/plugs/texnote/style.sty index fc97042..25e77d4 100644 --- a/papers/plugs/texnote/style.sty +++ b/papers/plugs/texnote/style.sty @@ -1,5 +1,3 @@ -\documentclass{article} - \usepackage{amsmath} \usepackage{amsfonts} \usepackage{amssymb} @@ -16,20 +14,3 @@ \usepackage{caption} \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} diff --git a/papers/plugs/texnote/template.tex b/papers/plugs/texnote/template.tex index fc97042..41aa3e0 100644 --- a/papers/plugs/texnote/template.tex +++ b/papers/plugs/texnote/template.tex @@ -1,21 +1,6 @@ \documentclass{article} -\usepackage{amsmath} -\usepackage{amsfonts} -\usepackage{amssymb} - -\usepackage{algorithm} -\usepackage{algorithmic} - -\usepackage{graphics} -\usepackage{graphicx} - -\usepackage{hyperref} -\usepackage{verbatim} -\usepackage{url} - -\usepackage{caption} -\usepackage{subcaption} +\usepackage{style} \begin{document} @@ -26,7 +11,7 @@ \end{center} \begin{abstract} - ABSTRACT + ABSTRACT \end{abstract} \section{Notes} diff --git a/papers/plugs/texnote/texnote.py b/papers/plugs/texnote/texnote.py index 3a78723..0f39bb6 100644 --- a/papers/plugs/texnote/texnote.py +++ b/papers/plugs/texnote/texnote.py @@ -64,76 +64,84 @@ class TexnotePlugin(PapersPlugin): del args.texcmd 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()) - key = parse_reference(ui, rp, reference) - print('Should remove {}'.format(key)) + paper = rp.get_paper(citekey) + 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): - print('Should edit {}'.format(reference)) if view is not None: subprocess.Popen(['papers', 'open', reference]) - #open_texnote(ui, reference) - - def edit_style(self): - pass + 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_template(self): - pass + def edit_style(self, ui): + files.edit_file(self.get_edit_cmd(), TEXNOTE_STYLE) - def toto(self): - print "toto" + def edit_template(self, ui): + files.edit_file(self.get_edit_cmd(), TEXNOTE_TEMPLATE) - #@RemoveEvent.listen() - def testEvent(self, rmevent): - print "testEvent" + def remove(self, ui, reference): + rp = repo.Repository(config()) + citekey = parse_reference(ui, rp, reference) + try: + os.remove(self._texfile(citekey)) + except OSError: + pass # For some reason, the texnote file didn't exist @RemoveEvent.listen() def remove(rmevent): texplug = TexnotePlugin.get_instance() 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 ##### @@ -162,38 +170,3 @@ def concatenate_authors(authors): concatenated += authors[a] 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()