Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved features #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions vizel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import six
from graphviz import Digraph

backlinks_header = "## Backlinks:"
title_regex = re.compile(r"^# (.+)", re.MULTILINE)


class Logger:
"""
Expand Down Expand Up @@ -156,10 +159,11 @@ def _load_references(zettel_content, zettel_path, zettel_directory_path):
return references


def _get_short_description(zettel_filename):
def _get_short_description(zettel_filename, zettel_directory_path):
"""
Creates a short description out of the Zettel filename.
:param zettel_filename: Filename of the Zettel
:param zettel_directory_path Path to directory where the Zettel are stored.
:return: 50 character long string
"""

Expand All @@ -168,6 +172,12 @@ def _get_short_description(zettel_filename):
remove = [".md", ".txt"]
short_des = zettel_filename

with open(os.path.join(zettel_directory_path, zettel_filename), "r") as file:
content = file.read()
title_match = title_regex.search(content)
if title_match:
short_des += f" {title_match.group(1)}"

for replace_char in replace_with_space:
short_des = short_des.replace(replace_char, " ")

Expand All @@ -177,6 +187,14 @@ def _get_short_description(zettel_filename):
return short_des


def remove_old_backlinks(content: str):
# remove backlinks block
backlinks_index = content.find(backlinks_header)
if backlinks_index != -1:
content = content[:backlinks_index].rstrip()
return content


def _get_digraph(zettel_directory_path):
"""
Parses the Zettel in `zettel_directory` and returns a digraph.
Expand All @@ -190,13 +208,13 @@ def _get_digraph(zettel_directory_path):
for zettel_path in sorted(
glob.glob(os.path.join(zettel_directory_path, "*[.md|.txt]"))
):

zettel_filename = os.path.basename(zettel_path)
short_des = _get_short_description(zettel_filename)
short_des = _get_short_description(zettel_filename, zettel_directory_path)
zettel_content = ""
try:
with open(zettel_path, "r") as zettel_file:
zettel_content = zettel_file.read()
zettel_content = remove_old_backlinks(zettel_content)
if six.PY2:
zettel_content = unicode(zettel_content, errors="strict")
except UnicodeDecodeError as e:
Expand Down Expand Up @@ -239,7 +257,7 @@ def _get_total_word_count(digraph):
"""

word_count = 0
for (node, data) in digraph.nodes(data=True):
for node, data in digraph.nodes(data=True):
previous_is_space = True
zettel_word_count = 0
for character in data["content"]:
Expand All @@ -255,26 +273,29 @@ def _get_total_word_count(digraph):

@main.command(short_help="PDF of Zettel graph")
@click.argument("directory", type=click.Path(exists=True, dir_okay=True))
@click.option("-q", "--quiet", is_flag=True, help="Quiet mode")
@click.option(
"--pdf-name",
default="vizel_graph.pdf",
help="Name of the PDF file the graph is written into. Default: vizel_graph.pdf",
)
def graph_pdf(directory, pdf_name):
def graph_pdf(directory, pdf_name, quiet):
"""
Generates a PDF of the graph spanned by Zettel in DIRECTORY.
\f

:param directory: Directory where all the Zettel are.
:param pdf_name: Name of the PDF file the graph is written into.
:param quiet: When set to True, warnings will not be printed.
:return None
"""

Logger.initialize(suppress_warnings=quiet)
digraph = _get_digraph(directory)

dot = Digraph(comment="Zettelkasten Graph")

for (node, data) in digraph.nodes(data=True):
for node, data in digraph.nodes(data=True):
dot.node(node, data["short_description"])

for u, v in digraph.edges:
Expand Down