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

View File

@ -1,6 +1,7 @@
import os import os
import json import json
import logging import logging
import re
from jinja2 import Environment, FileSystemLoader from jinja2 import Environment, FileSystemLoader
@ -11,6 +12,8 @@ from data.data_model import RenderCVDataModel
from tinytex.render import render from tinytex.render import render
if __name__ == "__main__": if __name__ == "__main__":
# logging config: # logging config:
logging.basicConfig( logging.basicConfig(
@ -24,6 +27,24 @@ if __name__ == "__main__":
environment = Environment( environment = Environment(
loader=FileSystemLoader(templatePath), trim_blocks=True, lstrip_blocks=True 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_start_string = "((*"
environment.block_end_string = "*))" environment.block_end_string = "*))"
environment.variable_start_string = "<<" environment.variable_start_string = "<<"

View File

@ -44,33 +44,33 @@
BoldItalicFont = *-BoldItalic] % set main font BoldItalicFont = *-BoldItalic] % set main font
\titleformat{\section}{ \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} \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 % from the end of the title to the end of the body
\textbf{#1}\hspace{0.15cm}\titlerule[0.8pt] \textbf{#1}\hspace{0.15cm}\titlerule[0.8pt]
}[] % Section title formatting }[] % section title formatting
\titlespacing{\section}{ \titlespacing{\section}{
% Left space: % left space:
0pt 0pt
}{ }{
% Top space: % top space:
<<design.section_title_top_margin>> <<design.section_title_top_margin>>
}{ }{
% Bottom space: % bottom space:
<<design.section_title_bottom_margin>> <<design.section_title_bottom_margin>>
} % Section title spacing } % section title spacing
\newcolumntype{L}[1]{ \newcolumntype{L}[1]{
>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1} >{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}
} % Left-aligned fixed width column type } % left-aligned fixed width column type
\newcolumntype{R}[1]{ \newcolumntype{R}[1]{
>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1} >{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}
} % Right-aligned fixed width column type } % right-aligned fixed width column type
\newenvironment{highlights}{ \newenvironment{highlights}{
\begin{itemize}[ \begin{itemize}[
@ -83,13 +83,15 @@
] ]
}{ }{
\end{itemize} \end{itemize}
} % New environment for highlights } % new environment for highlights
\newenvironment{header}{ \newenvironment{header}{
\setlength{\topsep}{0pt}\par\kern\topsep\centering\color{primaryColor}\linespread{1.5} \setlength{\topsep}{0pt}\par\kern\topsep\centering\color{primaryColor}\linespread{1.5}
}{ }{
\par\kern\topsep \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} \begin{document}
% Test out the 4 main entry types with the commands below: % Test out the 4 main entry types with the commands below:
@ -112,7 +114,7 @@
company=work.company, company=work.company,
position=work.position, position=work.position,
highlights=work.highlights, highlights=work.highlights,
date_and_location_strings=[work.start_date] date_and_location_strings=work.date_and_location_strings
)|indent(4)>> )|indent(4)>>
((* endfor *)) ((* endfor *))
\end{document} \end{document}

View File

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