From 9f1b5e981355d2393f36efc7eb872e8e61207fdf Mon Sep 17 00:00:00 2001 From: Jonathan Grizou Date: Mon, 8 Jul 2013 00:27:18 +0200 Subject: [PATCH] Extract note feature. Ready to compile several note together. --- papers/plugs/texnote/default_body.tex | 2 -- papers/plugs/texnote/latex_tools.py | 25 +++++++++++++++++++++++-- papers/plugs/texnote/texnote.py | 17 +++++++++++++++-- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/papers/plugs/texnote/default_body.tex b/papers/plugs/texnote/default_body.tex index 71c166c..a0f2da9 100644 --- a/papers/plugs/texnote/default_body.tex +++ b/papers/plugs/texnote/default_body.tex @@ -7,8 +7,6 @@ \usepackage{INFO} %The style location is automatically filled \begin{document} %} - - %HEADER{ %This part is the header, you can modify it as you wish. It will be removed when compiling higher level notes %TITLE, AUTHOR, YEAR, ABSTRACT will be automatyically replaced with respect to the associated bibfile thanks to the \autofill{*FIELD*}{} marker. diff --git a/papers/plugs/texnote/latex_tools.py b/papers/plugs/texnote/latex_tools.py index 007ce63..042eb28 100644 --- a/papers/plugs/texnote/latex_tools.py +++ b/papers/plugs/texnote/latex_tools.py @@ -1,6 +1,27 @@ import os import subprocess +from .autofill_tools import replace_pattern + +DO_NOT_MODIFY_PATTERN = '%DO_NOT_MODIFY{INFO}' +HEADER_PATTERN = '%HEADER{INFO}' + + +def extract_note(text): + text = replace_pattern(text, DO_NOT_MODIFY_PATTERN, 'INFO') + text = text.replace(DO_NOT_MODIFY_PATTERN, '') + text = replace_pattern(text, HEADER_PATTERN, 'INFO') + text = text.replace(HEADER_PATTERN, '') + return remove_empty_lines(text) + + +def remove_empty_lines(text): + cleaned_text = '' + for line in text.split('\n'): + if line.strip(): + cleaned_text += line + '\n' + return cleaned_text[:-1] + def full_compile(full_path_to_file, verbose=False): FNULL = None @@ -22,10 +43,10 @@ def run_command(command, full_path_to_file, stdout=None, nb_time=1): subprocess.call(cmd, stdout=stdout) os.chdir(origWD) # get back to our original working directory + def run_pdflatex(full_path_to_file, stdout=None, nb_time=1): run_command('pdflatex', full_path_to_file, stdout, nb_time) + def run_bibtex(full_path_to_file, stdout=None, nb_time=1): run_command('bibtex', full_path_to_file, stdout, nb_time) - - diff --git a/papers/plugs/texnote/texnote.py b/papers/plugs/texnote/texnote.py index 099fd85..ea61c53 100644 --- a/papers/plugs/texnote/texnote.py +++ b/papers/plugs/texnote/texnote.py @@ -13,8 +13,8 @@ from ...plugins import PapersPlugin from ...events import RemoveEvent, RenameEvent, AddEvent from ...commands.helpers import add_references_argument, parse_reference +from . import latex_tools from .autofill_tools import autofill, replace_pattern -from .latex_tools import full_compile SECTION = 'texnote' @@ -46,6 +46,7 @@ class TexnotePlugin(PapersPlugin): ('generate_bib', self.generate_bib), ('clean', self.clean), ('generate_pdf', self.generate_pdf), + ('extract_note', self.extract_note), ]) def _ensure_init(self): @@ -108,6 +109,10 @@ class TexnotePlugin(PapersPlugin): default=True, help="don't clean document afterwards") p.add_argument('-v', '--verbose', action='store_true', dest='verbose', default=False, help="display stdout") + # extract_note + p = sub.add_parser('extract_note', + help='extract core note from its reference') + add_references_argument(p, single=True) return parser def command(self, args): @@ -221,7 +226,7 @@ class TexnotePlugin(PapersPlugin): rp = repo.Repository(config()) citekey = parse_reference(rp, reference) path = self.get_texfile(citekey, autofill=True) - full_compile(path, verbose) + latex_tools.full_compile(path, verbose) if clean: self.clean(force=True) if open_pdf: @@ -231,6 +236,14 @@ class TexnotePlugin(PapersPlugin): cmd.append(os.path.splitext(path)[0] + '.pdf') subprocess.Popen(cmd) + def extract_note(self, reference): + rp = repo.Repository(config()) + citekey = parse_reference(rp, reference) + path = self.get_texfile(citekey, autofill=True) + with open(path) as f: + text = f.read() + print latex_tools.extract_note(text) + @AddEvent.listen() def create(addevent):