mirror of https://github.com/eyhc1/rendercv.git
fix emails with special LaTeX character
This commit is contained in:
parent
85408065ec
commit
3390df0ad6
|
@ -343,11 +343,12 @@ class MarkdownFile(TemplatedFile):
|
||||||
file_path.write_text(self.get_markdown_code(), encoding="utf-8")
|
file_path.write_text(self.get_markdown_code(), encoding="utf-8")
|
||||||
|
|
||||||
|
|
||||||
def escape_latex_characters(string: str) -> str:
|
def escape_latex_characters(string: str, strict: bool = True) -> str:
|
||||||
"""Escape $\\LaTeX$ characters in a string.
|
"""Escape $\\LaTeX$ characters in a string.
|
||||||
|
|
||||||
This function is called during the reading of the input file. Before the validation
|
This function is called during the reading of the input file. Before the validation
|
||||||
process, each input field's special $\\LaTeX$ characters are escaped.
|
process, each input field's special $\\LaTeX$ characters are escaped. It is also can
|
||||||
|
be used as a Jinja2 filter in templates.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
```python
|
```python
|
||||||
|
@ -355,21 +356,30 @@ def escape_latex_characters(string: str) -> str:
|
||||||
```
|
```
|
||||||
will return:
|
will return:
|
||||||
`#!python "This is a \\# string."`
|
`#!python "This is a \\# string."`
|
||||||
|
|
||||||
|
Args:
|
||||||
|
string (str): The string to escape.
|
||||||
|
strict (bool): Whether to escape all the special $\\LaTeX$ characters or not. If
|
||||||
|
you want to allow math input, set it to False.
|
||||||
|
Returns:
|
||||||
|
str: The escaped string.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Dictionary of escape characters:
|
# Dictionary of escape characters:
|
||||||
escape_characters = {
|
escape_characters = {
|
||||||
"#": "\\#",
|
"#": "\\#",
|
||||||
# "$": "\\$", # Don't escape $ as it is used for math mode
|
|
||||||
"%": "\\%",
|
"%": "\\%",
|
||||||
"&": "\\&",
|
"&": "\\&",
|
||||||
"~": "\\textasciitilde{}",
|
"~": "\\textasciitilde{}",
|
||||||
# "_": "\\_", # Don't escape _ as it is used for math mode
|
|
||||||
# "^": "\\textasciicircum{}", # Don't escape ^ as it is used for math mode
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Don't escape links as hyperref package will do it automatically:
|
if strict:
|
||||||
|
# To allow math input, users can use this function with strict = False
|
||||||
|
escape_characters["$"] = "\\$"
|
||||||
|
escape_characters["_"] = "\\_"
|
||||||
|
escape_characters["^"] = "\\textasciicircum{}"
|
||||||
|
|
||||||
|
# Don't escape links as hyperref package will do it automatically:
|
||||||
# Find all the links in the sentence:
|
# Find all the links in the sentence:
|
||||||
links = re.findall(r"\[.*?\]\(.*?\)", string)
|
links = re.findall(r"\[.*?\]\(.*?\)", string)
|
||||||
|
|
||||||
|
@ -482,20 +492,22 @@ def transform_markdown_sections_to_latex_sections(
|
||||||
for entry in value:
|
for entry in value:
|
||||||
if isinstance(entry, str):
|
if isinstance(entry, str):
|
||||||
# Then it means it's a TextEntry.
|
# Then it means it's a TextEntry.
|
||||||
result = markdown_to_latex(escape_latex_characters(entry))
|
result = markdown_to_latex(escape_latex_characters(entry, strict=False))
|
||||||
transformed_list.append(result)
|
transformed_list.append(result)
|
||||||
else:
|
else:
|
||||||
# Then it means it's one of the other entries.
|
# Then it means it's one of the other entries.
|
||||||
entry_as_dict = entry.model_dump()
|
entry_as_dict = entry.model_dump()
|
||||||
for entry_key, value in entry_as_dict.items():
|
for entry_key, value in entry_as_dict.items():
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
result = markdown_to_latex(escape_latex_characters(value))
|
result = markdown_to_latex(
|
||||||
|
escape_latex_characters(value, strict=False)
|
||||||
|
)
|
||||||
setattr(entry, entry_key, result)
|
setattr(entry, entry_key, result)
|
||||||
elif isinstance(value, list):
|
elif isinstance(value, list):
|
||||||
for j, item in enumerate(value):
|
for j, item in enumerate(value):
|
||||||
if isinstance(item, str):
|
if isinstance(item, str):
|
||||||
value[j] = markdown_to_latex(
|
value[j] = markdown_to_latex(
|
||||||
escape_latex_characters(item)
|
escape_latex_characters(item, strict=False)
|
||||||
)
|
)
|
||||||
setattr(entry, entry_key, value)
|
setattr(entry, entry_key, value)
|
||||||
transformed_list.append(entry)
|
transformed_list.append(entry)
|
||||||
|
@ -789,6 +801,7 @@ def setup_jinja2_environment() -> jinja2.Environment:
|
||||||
environment.filters["get_an_item_with_a_specific_attribute_value"] = (
|
environment.filters["get_an_item_with_a_specific_attribute_value"] = (
|
||||||
get_an_item_with_a_specific_attribute_value
|
get_an_item_with_a_specific_attribute_value
|
||||||
)
|
)
|
||||||
|
environment.filters["escape_latex_characters"] = escape_latex_characters
|
||||||
|
|
||||||
return environment
|
return environment
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@
|
||||||
\hspace*{<<design.margins.header.horizontal_between_connections>>}
|
\hspace*{<<design.margins.header.horizontal_between_connections>>}
|
||||||
((* endif *))
|
((* endif *))
|
||||||
((* if cv.email *))
|
((* if cv.email *))
|
||||||
\mbox{\hrefWithoutArrow{mailto:<<cv.email>>}{{\small\faEnvelope[regular]}\hspace*{0.13cm}<<cv.email>>}}
|
\mbox{\hrefWithoutArrow{mailto:<<cv.email>>}{{\small\faEnvelope[regular]}\hspace*{0.13cm}<<cv.email|escape_latex_characters>>}}
|
||||||
\hspace*{<<design.margins.header.horizontal_between_connections>>}
|
\hspace*{<<design.margins.header.horizontal_between_connections>>}
|
||||||
((* endif *))
|
((* endif *))
|
||||||
((* if cv.location *))
|
((* if cv.location *))
|
||||||
|
|
|
@ -54,7 +54,7 @@
|
||||||
\phone[mobile]{<<cv.phone|replace("tel:", "")|replace("-"," ")>>}
|
\phone[mobile]{<<cv.phone|replace("tel:", "")|replace("-"," ")>>}
|
||||||
((* endif *))
|
((* endif *))
|
||||||
((* if cv.email *))
|
((* if cv.email *))
|
||||||
\email{<<cv.email>>}
|
\email{<<cv.email|escape_latex_characters>>}
|
||||||
((* endif *))
|
((* endif *))
|
||||||
((* if cv.website *))
|
((* if cv.website *))
|
||||||
\homepage{<<cv.website|replace("https://", "")|reverse|replace("/", "", 1)|reverse>>}
|
\homepage{<<cv.website|replace("https://", "")|reverse|replace("/", "", 1)|reverse>>}
|
||||||
|
|
|
@ -23,7 +23,7 @@
|
||||||
\hspace{<<design.margins.header.horizontal_between_connections>>}
|
\hspace{<<design.margins.header.horizontal_between_connections>>}
|
||||||
((* endif *))
|
((* endif *))
|
||||||
((* if cv.email *))
|
((* if cv.email *))
|
||||||
\mbox{\href{mailto:<<cv.email>>}{{\small\faEnvelope[regular]}\hspace{4pt}<<cv.email>>}}
|
\mbox{\href{mailto:<<cv.email>>}{{\small\faEnvelope[regular]}\hspace{4pt}<<cv.email|escape_latex_characters>>}}
|
||||||
\hspace{<<design.margins.header.horizontal_between_connections>>}
|
\hspace{<<design.margins.header.horizontal_between_connections>>}
|
||||||
((* endif *))
|
((* endif *))
|
||||||
((* if cv.location *))
|
((* if cv.location *))
|
||||||
|
|
Loading…
Reference in New Issue