add markdown_to_latex filter to jinja

This commit is contained in:
Sina Atalay 2023-09-07 22:09:56 +02:00
parent 3d4828660f
commit 93e39686d3
4 changed files with 42 additions and 16 deletions

View File

@ -3,6 +3,7 @@ from typing import Literal, Union
from typing_extensions import Annotated
import re
import logging
import math
from functools import cached_property
from pydantic import BaseModel, HttpUrl, model_validator, computed_field
@ -70,9 +71,12 @@ def compute_time_span_string(start_date: Date, end_date: Date) -> str:
else:
howManyYearsString = f"{howManyYears} years"
howManyMonths = (timeSpan % 365) // 30
howManyMonths = math.ceil((timeSpan % 365) / 30)
if howManyMonths == 0:
howManyYearsString = None
howManyMonths = 1
if howManyMonths == 0:
howManyMonthsString = None
elif howManyMonths == 1:
howManyMonthsString = "1 month"
else:
@ -128,7 +132,6 @@ class Event(BaseModel):
end_date: Date | Literal["present"] = None
date: str = None
location: str
# date_and_location_strings: list[str] = []
@model_validator(mode="after")
@classmethod

View File

@ -1,6 +1,7 @@
import os
import json
import logging
import re
from jinja2 import Environment, FileSystemLoader
@ -11,6 +12,8 @@ from data.data_model import RenderCVDataModel
from tinytex.render import render
if __name__ == "__main__":
# logging config:
logging.basicConfig(
@ -24,6 +27,24 @@ if __name__ == "__main__":
environment = Environment(
loader=FileSystemLoader(templatePath), trim_blocks=True, lstrip_blocks=True
)
def markdown_to_latex(value: str) -> str:
"""
To be continued...
"""
# convert links
link = re.search("\[(.*)\]\((.*?)\)", value)
if link is not None:
link = link.groups()
oldLinkString = "[" + link[0] + "](" + link[1] + ")"
newLinkString = "\hrefExternal{" + link[1] + "}{" + link[0] + "}"
value = value.replace(oldLinkString, newLinkString)
return value
environment.filters["markdown_to_latex"] = markdown_to_latex
environment.block_start_string = "((*"
environment.block_end_string = "*))"
environment.variable_start_string = "<<"

View File

@ -44,33 +44,33 @@
BoldItalicFont = *-BoldItalic] % set main font
\titleformat{\section}{
% Make the font size of the section title large and color it with the primary color
% make the font size of the section title large and color it with the primary color
\LARGE\color{primaryColor}
}{
}{
}{
% Print bold title, give 0.15cm space and draw a line of 0.8pt thickness
% print bold title, give 0.15 cm space and draw a line of 0.8 pt thickness
% from the end of the title to the end of the body
\textbf{#1}\hspace{0.15cm}\titlerule[0.8pt]
}[] % Section title formatting
}[] % section title formatting
\titlespacing{\section}{
% Left space:
% left space:
0pt
}{
% Top space:
% top space:
<<design.section_title_top_margin>>
}{
% Bottom space:
% bottom space:
<<design.section_title_bottom_margin>>
} % Section title spacing
} % section title spacing
\newcolumntype{L}[1]{
>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}
} % Left-aligned fixed width column type
} % left-aligned fixed width column type
\newcolumntype{R}[1]{
>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}
} % Right-aligned fixed width column type
} % right-aligned fixed width column type
\newenvironment{highlights}{
\begin{itemize}[
@ -83,13 +83,15 @@
]
}{
\end{itemize}
} % New environment for highlights
} % new environment for highlights
\newenvironment{header}{
\setlength{\topsep}{0pt}\par\kern\topsep\centering\color{primaryColor}\linespread{1.5}
}{
\par\kern\topsep
} % New environment for the header
} % new environment for the header
\newcommand{\hrefExternal}[2]{\href{#1}{#2\, \raisebox{.1ex}{\footnotesize \faExternalLink*}}} % new command for external links
\begin{document}
% Test out the 4 main entry types with the commands below:
@ -112,7 +114,7 @@
company=work.company,
position=work.position,
highlights=work.highlights,
date_and_location_strings=[work.start_date]
date_and_location_strings=work.date_and_location_strings
)|indent(4)>>
((* endfor *))
\end{document}

View File

@ -2,7 +2,7 @@
\vspace{<<design.vertical_margin_between_entries_and_highlights>>}
\begin{highlights}
((* for item in highlights *))
\item <<item>>
\item <<item|markdown_to_latex>>
((* endfor *))
\end{highlights}
((* endmacro *))