Add formatting style Markdown

This commit is contained in:
Marty Oehme 2023-08-31 21:32:24 +02:00
parent 20873e6ef8
commit e56f014136
Signed by: Marty
GPG key ID: EDBF2ED917B2EF6A
4 changed files with 42 additions and 17 deletions

View file

@ -8,7 +8,7 @@ import papis.strings
from papis.document import Document
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__)
@ -84,6 +84,6 @@ def run(
doc_annotations: list[AnnotatedDocument] = extractor.start(documents)
if write:
exporter.to_notes(doc_annotations, edit=edit, git=git)
exporter.to_notes(doc_annotations, Markdown(), edit=edit, git=git)
else:
exporter.to_stdout(doc_annotations)
exporter.to_stdout(doc_annotations, Markdown())

View file

@ -1,5 +1,6 @@
import math
from dataclasses import dataclass, field
from typing import Protocol
import papis.config
from papis.document import Document
@ -55,9 +56,7 @@ class Annotation:
Finds the closest named color to the annotation and returns it,
using euclidian distance between the two color vectors.
"""
annot_colors = (
self.colors or (0.0, 0.0, 0.0)
)
annot_colors = self.colors or (0.0, 0.0, 0.0)
nearest = None
minimum_similarity = (
papis.config.getfloat("minimum_similarity_color", "plugins.extract") or 1.0
@ -85,3 +84,17 @@ class Annotation:
class AnnotatedDocument:
document: Document
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}}"
)

View file

@ -7,17 +7,12 @@ import papis.git
import papis.config
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__)
def _format_annotation(annotation: Annotation) -> str:
note = f"NOTE: {annotation.content}" if annotation.content else ""
return f"> {annotation.text}\n {note}"
def to_stdout(annots: list[AnnotatedDocument]) -> None:
def to_stdout(annots: list[AnnotatedDocument], template: Templating) -> None:
"""Pretty print annotations to stdout.
Gives a nice human-readable representations of
@ -27,6 +22,7 @@ def to_stdout(annots: list[AnnotatedDocument]) -> None:
if not annots:
return
last = annots[-1]
for entry in annots:
if not entry.annotations:
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"
)
for a in entry.annotations:
print(_format_annotation(a))
print(a.format(template.string))
if entry != annots[-1]:
if entry != last:
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.
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] = []
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)

14
tests/test_formatting.py Normal file
View 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."