Skip to content

Commit 1f5180a

Browse files
fix latex text formatting
1 parent 5b1bcc3 commit 1f5180a

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

src/feelpp/benchmarking/json_report/templates/tex/json2tex_report.tex.j2

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
\usepackage{pgfplots}
88
\usepackage{pgfplotstable}
99
\usepackage{underscore}
10+
\usepackage{amsfonts}
11+
\usepackage{hyperref}
12+
\usepackage{xurl}
1013
\usepgfplotslibrary{fillbetween}
1114

1215
{% for package in report.extra_packages %}

src/feelpp/benchmarking/json_report/templates/tex/macros/table.tex.j2

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
{% import "macros/text.tex.j2" as text %}
22

3-
3+
{% macro escapeSpecialChars(text) -%}
4+
{{ text | string
5+
| replace('\\', '\textbackslash{}')
6+
| replace('{', '\{')
7+
| replace('}', '\}')
8+
| replace('#', '\#')
9+
| replace('$', '\$')
10+
| replace('%', '\%')
11+
| replace('&', '\&')
12+
| replace('_', '\_')
13+
| replace('~', '\textasciitilde{}')
14+
| replace('^', '\^{}')
15+
}}
16+
{%- endmacro %}
417

518
{% macro render(tableNode, data) %}
619

@@ -13,10 +26,10 @@
1326
\centering
1427
\begin{tabular}{ {{ cols_align }} }
1528
\hline
16-
{% for col in table.columns %}{{ col }}{% if not loop.last %} & {% endif %}{% endfor %} \\
29+
{% for col in table.columns %}{{ escapeSpecialChars(col) }}{% if not loop.last %} & {% endif %}{% endfor %} \\
1730
\hline
1831
{% for row in table.itertuples(index=False) %}
19-
{% for cell in row %}{{ cell }}{% if not loop.last %} & {% endif %}{% endfor %} \\
32+
{% for cell in row %}{{ escapeSpecialChars(cell) }}{% if not loop.last %} & {% endif %}{% endfor %} \\
2033
{% endfor %}
2134
\hline
2235
\end{tabular}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{% macro render(textNode,data) %}
2-
{{ TextController((data or {}).get(textNode.ref),textNode.text).generate() }}
2+
{{ TextController((data or {}).get(textNode.ref),textNode.text).generate(format="tex") }}
33
{% endmacro %}

src/feelpp/benchmarking/json_report/text/controller.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,52 @@ def __init__(self, data, text:Text):
77
self.text_config = text
88
self.data = data
99

10-
def generate(self):
10+
@staticmethod
11+
def _asciidoc_to_latex_urls(text: str) -> str:
12+
pattern = re.compile(r'([(<]*)(\S+?)\[(.*?)\]([)>.,;!?:]*)')
13+
14+
def repl(m):
15+
pre, url, label, post = m.groups()
16+
url = url.strip()
17+
label = label.strip()
18+
if label:
19+
replacement = r'\href{' + url + '}{' + label + '}'
20+
else:
21+
replacement = r'\url{' + url + '}'
22+
return pre + replacement + post
23+
24+
return pattern.sub(repl, text)
25+
26+
27+
def formatText(self,text:str,format = "adoc"):
28+
if format == "adoc":
29+
pass
30+
elif format == "tex":
31+
# stem
32+
text = re.sub( r"stem:\[\s*(.*?)\s*\]", r"$\1$", text )
33+
34+
#italics
35+
text = re.sub(r"(?<![A-Za-z0-9])_(\S(?:.*?\S)?)_(?![A-Za-z0-9])", r"\\textit{\1}", text )
36+
37+
#bold
38+
text = re.sub(r"(?<![A-Za-z0-9])\*(.+?)\*(?![A-Za-z0-9])", r"\\textbf{\1}", text )
39+
40+
#underline
41+
text = re.sub(r"r'\[\.underline\]#(.*?)#'", r"\\underline{\1}", text )
42+
43+
#links
44+
text = self._asciidoc_to_latex_urls(text)
45+
46+
47+
else:
48+
raise NotImplementedError(f"Format not recognized in text controller: {format}")
49+
50+
return text
51+
52+
53+
54+
55+
def generate(self, format="adoc"):
1156
if self.text_config.mode == "static":
1257
content = self.text_config.content
1358

@@ -23,7 +68,7 @@ def generate(self):
2368
else:
2469
content = self.text_config.content
2570

26-
return content
71+
return self.formatText(str(content),format)
2772

2873
def _resolvePlaceHolders(self, match):
2974
expr = match.group(1)

0 commit comments

Comments
 (0)