Add formatting style Markdown
This commit is contained in:
parent
20873e6ef8
commit
e56f014136
4 changed files with 42 additions and 17 deletions
|
@ -8,7 +8,7 @@ import papis.strings
|
||||||
from papis.document import Document
|
from papis.document import Document
|
||||||
|
|
||||||
from papis_extract import extractor, exporter
|
from papis_extract import extractor, exporter
|
||||||
from papis_extract.annotation_data import AnnotatedDocument
|
from papis_extract.annotation_data import AnnotatedDocument, Markdown
|
||||||
|
|
||||||
logger = papis.logging.get_logger(__name__)
|
logger = papis.logging.get_logger(__name__)
|
||||||
|
|
||||||
|
@ -84,6 +84,6 @@ def run(
|
||||||
doc_annotations: list[AnnotatedDocument] = extractor.start(documents)
|
doc_annotations: list[AnnotatedDocument] = extractor.start(documents)
|
||||||
|
|
||||||
if write:
|
if write:
|
||||||
exporter.to_notes(doc_annotations, edit=edit, git=git)
|
exporter.to_notes(doc_annotations, Markdown(), edit=edit, git=git)
|
||||||
else:
|
else:
|
||||||
exporter.to_stdout(doc_annotations)
|
exporter.to_stdout(doc_annotations, Markdown())
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import math
|
import math
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
|
from typing import Protocol
|
||||||
|
|
||||||
import papis.config
|
import papis.config
|
||||||
from papis.document import Document
|
from papis.document import Document
|
||||||
|
@ -55,9 +56,7 @@ class Annotation:
|
||||||
Finds the closest named color to the annotation and returns it,
|
Finds the closest named color to the annotation and returns it,
|
||||||
using euclidian distance between the two color vectors.
|
using euclidian distance between the two color vectors.
|
||||||
"""
|
"""
|
||||||
annot_colors = (
|
annot_colors = self.colors or (0.0, 0.0, 0.0)
|
||||||
self.colors or (0.0, 0.0, 0.0)
|
|
||||||
)
|
|
||||||
nearest = None
|
nearest = None
|
||||||
minimum_similarity = (
|
minimum_similarity = (
|
||||||
papis.config.getfloat("minimum_similarity_color", "plugins.extract") or 1.0
|
papis.config.getfloat("minimum_similarity_color", "plugins.extract") or 1.0
|
||||||
|
@ -85,3 +84,17 @@ class Annotation:
|
||||||
class AnnotatedDocument:
|
class AnnotatedDocument:
|
||||||
document: Document
|
document: Document
|
||||||
annotations: list[Annotation]
|
annotations: list[Annotation]
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Templating(Protocol):
|
||||||
|
string: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Markdown:
|
||||||
|
string: str = (
|
||||||
|
"{{#tag}}#{{tag}}\n{{/tag}}"
|
||||||
|
"{{#quote}}> {{quote}}{{/quote}} {{#page}}[p. {{page}}]{{/page}}\n"
|
||||||
|
"{{#note}} NOTE: {{note}}{{/note}}"
|
||||||
|
)
|
||||||
|
|
|
@ -7,17 +7,12 @@ import papis.git
|
||||||
import papis.config
|
import papis.config
|
||||||
import Levenshtein
|
import Levenshtein
|
||||||
|
|
||||||
from papis_extract.annotation_data import AnnotatedDocument, Annotation
|
from papis_extract.annotation_data import AnnotatedDocument, Annotation, Templating
|
||||||
|
|
||||||
logger = papis.logging.get_logger(__name__)
|
logger = papis.logging.get_logger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def _format_annotation(annotation: Annotation) -> str:
|
def to_stdout(annots: list[AnnotatedDocument], template: Templating) -> None:
|
||||||
note = f"NOTE: {annotation.content}" if annotation.content else ""
|
|
||||||
return f"> {annotation.text}\n {note}"
|
|
||||||
|
|
||||||
|
|
||||||
def to_stdout(annots: list[AnnotatedDocument]) -> None:
|
|
||||||
"""Pretty print annotations to stdout.
|
"""Pretty print annotations to stdout.
|
||||||
|
|
||||||
Gives a nice human-readable representations of
|
Gives a nice human-readable representations of
|
||||||
|
@ -27,6 +22,7 @@ def to_stdout(annots: list[AnnotatedDocument]) -> None:
|
||||||
if not annots:
|
if not annots:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
last = annots[-1]
|
||||||
for entry in annots:
|
for entry in annots:
|
||||||
if not entry.annotations:
|
if not entry.annotations:
|
||||||
continue
|
continue
|
||||||
|
@ -39,13 +35,15 @@ def to_stdout(annots: list[AnnotatedDocument]) -> None:
|
||||||
f"{title_decoration}\n{papis.document.describe(entry.document)}\n{title_decoration}\n"
|
f"{title_decoration}\n{papis.document.describe(entry.document)}\n{title_decoration}\n"
|
||||||
)
|
)
|
||||||
for a in entry.annotations:
|
for a in entry.annotations:
|
||||||
print(_format_annotation(a))
|
print(a.format(template.string))
|
||||||
|
|
||||||
if entry != annots[-1]:
|
if entry != last:
|
||||||
print("\n")
|
print("\n")
|
||||||
|
|
||||||
|
|
||||||
def to_notes(annots: list[AnnotatedDocument], edit: bool, git: bool) -> None:
|
def to_notes(
|
||||||
|
annots: list[AnnotatedDocument], template: Templating, edit: bool, git: bool
|
||||||
|
) -> None:
|
||||||
"""Write annotations into document notes.
|
"""Write annotations into document notes.
|
||||||
|
|
||||||
Permanently writes the given annotations into notes
|
Permanently writes the given annotations into notes
|
||||||
|
@ -61,7 +59,7 @@ def to_notes(annots: list[AnnotatedDocument], edit: bool, git: bool) -> None:
|
||||||
|
|
||||||
formatted_annotations: list[str] = []
|
formatted_annotations: list[str] = []
|
||||||
for a in entry.annotations:
|
for a in entry.annotations:
|
||||||
formatted_annotations.append(_format_annotation(a))
|
formatted_annotations.append(a.format(template.string))
|
||||||
|
|
||||||
_add_annots_to_note(entry.document, formatted_annotations)
|
_add_annots_to_note(entry.document, formatted_annotations)
|
||||||
|
|
||||||
|
|
14
tests/test_formatting.py
Normal file
14
tests/test_formatting.py
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
import chevron
|
||||||
|
|
||||||
|
from papis_extract.annotation_data import Markdown
|
||||||
|
|
||||||
|
def test_markdown_default():
|
||||||
|
fmt = Markdown()
|
||||||
|
assert chevron.render(fmt.string, {
|
||||||
|
"file": "somefile/somewhere.pdf",
|
||||||
|
"quote": "I am quote",
|
||||||
|
"note": "and including note.",
|
||||||
|
"page": 46,
|
||||||
|
"tag": "important",
|
||||||
|
"type": "highlight",
|
||||||
|
}) == "#important\n> I am quote [p. 46]\n NOTE: and including note."
|
Loading…
Reference in a new issue