Added templating for render
This commit is contained in:
parent
44549aaff3
commit
af14c80803
@ -1,6 +1,6 @@
|
|||||||
from pyn.common import parser, logger
|
from pyn.common import parser, logger
|
||||||
from pyn.config import Config
|
from pyn.config import Config
|
||||||
from pyn import notes
|
from pyn import notes, templating
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
@ -57,7 +57,7 @@ def cmd_del_note(config: Config, title: str, notebook: str, yes: bool) -> None:
|
|||||||
del_note = group.add_parser("delete", aliases=["del"])
|
del_note = group.add_parser("delete", aliases=["del"])
|
||||||
del_note.add_argument("--notebook", "-n", help="add to this notebook [general]")
|
del_note.add_argument("--notebook", "-n", help="add to this notebook [general]")
|
||||||
del_note.add_argument("--yes", action="store_true", help="dont prompt for confirmation")
|
del_note.add_argument("--yes", action="store_true", help="dont prompt for confirmation")
|
||||||
del_note.add_argument("title", help="title for note", nargs="?")
|
del_note.add_argument("title", help="title for note", nargs="*")
|
||||||
del_note.set_defaults(func=cmd_del_note)
|
del_note.set_defaults(func=cmd_del_note)
|
||||||
|
|
||||||
### List notes
|
### List notes
|
||||||
@ -89,7 +89,7 @@ def cmd_edit_note(config, title, notebook: str) -> None:
|
|||||||
|
|
||||||
|
|
||||||
edit_note = group.add_parser("edit", aliases=["e"])
|
edit_note = group.add_parser("edit", aliases=["e"])
|
||||||
edit_note.add_argument("title", help="note title", nargs="?")
|
edit_note.add_argument("title", help="note title", nargs="*")
|
||||||
edit_note.add_argument("--notebook", "-n", help="limit to this notebook")
|
edit_note.add_argument("--notebook", "-n", help="limit to this notebook")
|
||||||
edit_note.set_defaults(func=cmd_edit_note)
|
edit_note.set_defaults(func=cmd_edit_note)
|
||||||
|
|
||||||
@ -102,28 +102,24 @@ def cmd_show_note(config, title, notebook: str) -> None:
|
|||||||
|
|
||||||
|
|
||||||
show_note = group.add_parser("show", aliases=["s", "cat"])
|
show_note = group.add_parser("show", aliases=["s", "cat"])
|
||||||
show_note.add_argument("title", help="note title", nargs="?")
|
show_note.add_argument("title", help="note title", nargs="*")
|
||||||
show_note.add_argument("--notebook", "-n", help="limit to this notebook")
|
show_note.add_argument("--notebook", "-n", help="limit to this notebook")
|
||||||
show_note.set_defaults(func=cmd_show_note)
|
show_note.set_defaults(func=cmd_show_note)
|
||||||
|
|
||||||
|
|
||||||
def cmd_render_note(config, title, notebook: str) -> None:
|
def cmd_render_note(config, title, notebook: str) -> None:
|
||||||
filename = notes.search_filenames(config, title, notebook)
|
filename = notes.search_filenames(config, title, notebook)
|
||||||
import markdown
|
|
||||||
import tempfile
|
|
||||||
import subprocess
|
|
||||||
import time
|
|
||||||
|
|
||||||
with open(filename, "r", encoding="utf-8") as input_file:
|
with open(filename, "r", encoding="utf-8") as input_file:
|
||||||
text = input_file.read()
|
text = input_file.read()
|
||||||
html = markdown.markdown(text)
|
|
||||||
|
|
||||||
with tempfile.NamedTemporaryFile(suffix=".html", delete=True) as f:
|
html = templating.render_note(text, title=filename.stem)
|
||||||
f.write(html.encode('utf-8'))
|
|
||||||
subprocess.run(['firefox', '--new-tab', f"file://{f.name}"])
|
print(html)
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
render_note = group.add_parser("render")
|
render_note = group.add_parser("render")
|
||||||
render_note.add_argument("title", help="note title", nargs="?")
|
render_note.add_argument("title", help="note title", nargs="*")
|
||||||
render_note.add_argument("--notebook", "-n", help="limit to this notebook")
|
render_note.add_argument("--notebook", "-n", help="limit to this notebook")
|
||||||
render_note.set_defaults(func=cmd_render_note)
|
render_note.set_defaults(func=cmd_render_note)
|
||||||
|
|
||||||
|
|||||||
12
pyn/notes.py
12
pyn/notes.py
@ -77,19 +77,25 @@ def get_file_list(
|
|||||||
dirs[:] = [d for d in dirs if d[0] != "."]
|
dirs[:] = [d for d in dirs if d[0] != "."]
|
||||||
|
|
||||||
for filename in files:
|
for filename in files:
|
||||||
yield d / filename
|
if filename.endswith(".md"):
|
||||||
|
yield d.relative_to(root) / filename
|
||||||
|
|
||||||
|
|
||||||
def search_filenames(
|
def search_filenames(
|
||||||
config: Config, title: str, notebook: str | None = None
|
config: Config, title: str | list[str], notebook: str | None = None
|
||||||
) -> pathlib.Path:
|
) -> pathlib.Path:
|
||||||
root = config.directory / notebook if notebook else config.directory
|
root = config.directory / notebook if notebook else config.directory
|
||||||
|
|
||||||
|
files = "\n".join((str(x) for x in get_file_list(config, notebook)))
|
||||||
|
|
||||||
|
if isinstance(title, list):
|
||||||
|
title = " ".join(title)
|
||||||
|
|
||||||
cmd = ["fzf", "-1", "-i"]
|
cmd = ["fzf", "-1", "-i"]
|
||||||
if title:
|
if title:
|
||||||
cmd.extend(["-q", title])
|
cmd.extend(["-q", title])
|
||||||
try:
|
try:
|
||||||
output = subprocess.check_output(cmd, cwd=root)
|
output = subprocess.check_output(cmd, cwd=root, input=files.encode())
|
||||||
file_path = root / output.decode("utf-8").strip()
|
file_path = root / output.decode("utf-8").strip()
|
||||||
return file_path
|
return file_path
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
|
|||||||
43
pyn/templates/render_note.html
Normal file
43
pyn/templates/render_note.html
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
<!doctype html5>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>{{ title }}</title>
|
||||||
|
<style>
|
||||||
|
BODY {
|
||||||
|
font-family: Arial, Helvetica, sans-serif;
|
||||||
|
font-size: 10pt;
|
||||||
|
}
|
||||||
|
DIV.page {
|
||||||
|
margin: 20px;
|
||||||
|
}
|
||||||
|
HR {
|
||||||
|
color: #DDD;
|
||||||
|
}
|
||||||
|
H1, H2, H3 {
|
||||||
|
margin-bottom: 5px;
|
||||||
|
}
|
||||||
|
P {
|
||||||
|
margin-top: 2px;
|
||||||
|
margin-bottom: 2px;
|
||||||
|
}
|
||||||
|
H1 {
|
||||||
|
font-size: 16pt;
|
||||||
|
}
|
||||||
|
H2 {
|
||||||
|
font-size: 14pt;
|
||||||
|
}
|
||||||
|
H3 {
|
||||||
|
font-size: 12pt;
|
||||||
|
}
|
||||||
|
DIV.page P:first-of-type {
|
||||||
|
color: #999;
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="page">
|
||||||
|
{{ content|safe }}
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
11
pyn/templating.py
Normal file
11
pyn/templating.py
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
from jinja2 import Environment, PackageLoader, select_autoescape
|
||||||
|
import markdown
|
||||||
|
|
||||||
|
env = Environment(loader=PackageLoader("pyn"), autoescape=select_autoescape())
|
||||||
|
|
||||||
|
|
||||||
|
def render_note(note: str, title: str = "") -> str:
|
||||||
|
content = markdown.markdown(note)
|
||||||
|
|
||||||
|
template = env.get_template("render_note.html")
|
||||||
|
return template.render(content=content, title=title)
|
||||||
@ -8,7 +8,8 @@ authors = [
|
|||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
requires-python = ">=3.13"
|
requires-python = ">=3.13"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"markdown (>=3.10.1,<4.0.0)"
|
"markdown (>=3.10.1,<4.0.0)",
|
||||||
|
"jinja2 (>=3.1.6,<4.0.0)"
|
||||||
]
|
]
|
||||||
|
|
||||||
[project.scripts]
|
[project.scripts]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user