From 93e39686d3c6488f1e5f3494a1dedeba19015fd5 Mon Sep 17 00:00:00 2001 From: Sina Atalay Date: Thu, 7 Sep 2023 22:09:56 +0200 Subject: [PATCH] add markdown_to_latex filter to jinja --- rendercv/data/content.py | 9 ++++--- rendercv/rendercv.py | 21 +++++++++++++++ rendercv/templates/classic/classic.tex.j2 | 26 ++++++++++--------- .../classic/components/highlights.tex.j2 | 2 +- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/rendercv/data/content.py b/rendercv/data/content.py index 9301d67..d2a24d2 100644 --- a/rendercv/data/content.py +++ b/rendercv/data/content.py @@ -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 diff --git a/rendercv/rendercv.py b/rendercv/rendercv.py index 81117ba..3959444 100644 --- a/rendercv/rendercv.py +++ b/rendercv/rendercv.py @@ -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 = "<<" diff --git a/rendercv/templates/classic/classic.tex.j2 b/rendercv/templates/classic/classic.tex.j2 index f56545c..822b44e 100644 --- a/rendercv/templates/classic/classic.tex.j2 +++ b/rendercv/templates/classic/classic.tex.j2 @@ -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: <> }{ - % Bottom space: + % bottom space: <> - } % 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} \ No newline at end of file diff --git a/rendercv/templates/classic/components/highlights.tex.j2 b/rendercv/templates/classic/components/highlights.tex.j2 index f6db1a1..55df513 100644 --- a/rendercv/templates/classic/components/highlights.tex.j2 +++ b/rendercv/templates/classic/components/highlights.tex.j2 @@ -2,7 +2,7 @@ \vspace{<>} \begin{highlights} ((* for item in highlights *)) - \item <> + \item <> ((* endfor *)) \end{highlights} ((* endmacro *)) \ No newline at end of file