Skip to content

Commit

Permalink
Merge pull request #36 from ppizarror/fix-quotes-replace
Browse files Browse the repository at this point in the history
Fix quote replace
  • Loading branch information
ppizarror authored Dec 5, 2022
2 parents aa81db6 + 8edc31a commit 4fadf3c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 51 deletions.
51 changes: 19 additions & 32 deletions pydetex/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
'process_inputs',
'process_items',
'process_labels',
'process_quotes',
'process_ref',
'remove_commands_char',
'remove_commands_param',
Expand All @@ -37,7 +36,7 @@
import pydetex.utils as ut

from pydetex._symbols import *
from typing import List, Tuple, Union, Optional
from typing import List, Tuple, Union, Optional, Callable

# Files
_LAST_NOT_FOUND_FILES_PATH = [os.getcwd()]
Expand Down Expand Up @@ -434,28 +433,6 @@ def process_ref(s: str, **kwargs) -> str:
break


def process_quotes(s: str, **kwargs) -> str:
"""
Process quotes.
:param s: Latex string code
:return: String with "quotes"
"""
while True:
k = find_str(s, ['\\quotes{', '\\doublequotes{', '\\enquote{'])
if k == -1:
if kwargs.get('pb'): # Update progressbar
kwargs.get('pb').update('Processing quotes')
return s
m = 0
for j in range(len(s)):
if s[k + j] == '{':
m = j
if s[k + j] == '}':
s = s[:k] + '"' + s[k + m + 1:k + j] + '"' + s[k + j + 1:]
break


def remove_comments(s: str, **kwargs) -> str:
"""
Remove comments from text.
Expand Down Expand Up @@ -723,21 +700,27 @@ def output_text_for_some_commands(
# The font format is like .... [font tag]YOUR TAG {[font content]YOUR CONTENT} ...[font normal]. In that case, tag to be
# relaced is 'YOUR TAG {0}, {1}
# All *arguments will be formatted using the tag
commands: List[Tuple[str, List[Tuple[int, bool]], str, int, Optional[str], Optional[str], Tuple[bool, bool]]] = [
commands: List[Tuple[str, List[Tuple[int, bool]], Union[str, Callable[[str, ...], str]], int, Optional[str], Optional[str], Tuple[bool, bool]]] = [
('caption', [(1, False)], LANG_TT_TAGS.get(lang, 'caption'), 1, None, None, (False, True)),
('chapter', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('chapter*', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('doublequotes', [(1, False)], lambda t: '"{0}"'.format(t), 1, 'normal', 'normal', (False, False)),
('em', [(1, False)], '{0}', 1, 'normal', 'bold', (False, False)),
('emph', [(1, False)], '{0}', 1, 'normal', 'italic', (False, False)),
('enquote', [(1, False)], lambda t: '"{0}"'.format(t), 1, 'normal', 'normal', (False, False)),
('href', [(2, False)], LANG_TT_TAGS.get(lang, 'link'), 2, None, None, (False, False)),
('insertimage', [(3, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 3, None, None, (False, True)),
('insertimage', [(4, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 4, None, None, (False, False)),
('insertimageboxed', [(4, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 4, None, None, (False, True)),
('insertimageboxed', [(5, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 5, None, None, (False, True)),
('insertimage', [(3, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 3, None, None, (False, True)), # Template-Informe
('insertimage', [(4, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 4, None, None, (False, False)), # Template-Informe
('insertimageboxed', [(4, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 4, None, None, (False, True)), # Template-Informe
('insertimageboxed', [(5, False)], LANG_TT_TAGS.get(lang, 'figure_caption'), 5, None, None, (False, True)), # Template-Informe
('institutionentry', [(1, False), (2, False), (3, False), (4, False)], '{0} ({1}-{2}). {3}', 4, 'normal', 'normal', (False, False)), # Professional-CV
('institutionentryshort', [(1, False), (2, False), (3, False), (4, False)], '{0} ({1}-{2}). {3}', 4, 'normal', 'normal', (False, False)), # Professional-CV
('lowercase', [(1, False)], lambda t: t.lower(), 1, 'normal', 'normal', (False, False)),
('MakeLowercase', [(1, False)], lambda t: t.lower(), 1, 'normal', 'normal', (False, False)),
('MakeUppercase', [(1, False)], lambda t: t.upper(), 1, 'normal', 'normal', (False, False)),
('otherentry', [(1, False), (2, False)], '{0} {1}', 2, 'normal', 'normal', (False, False)), # Professional-CV
('paragraph', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('quotes', [(1, False)], lambda t: '"{0}"'.format(t), 1, 'normal', 'normal', (False, False)),
('section', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('section*', [(1, False)], '{0}', 1, 'normal', 'bold', (True, True)),
('subfloat', [(1, True)], LANG_TT_TAGS.get(lang, 'sub_figure_title'), 1, None, None, (False, True)),
Expand Down Expand Up @@ -782,7 +765,10 @@ def output_text_for_some_commands(
if callable(cmd_tag):
text = cmd_tag(*args)
else:
text = cmd_tag.format(*args)
try:
text = cmd_tag.format(*args)
except IndexError:
text = cmd_tag
text = FONT_FORMAT_SETTINGS[font_tag] + text + FONT_FORMAT_SETTINGS['normal']
if cmd_newline[0]:
text = _TAG_NEW_LINE + text
Expand Down Expand Up @@ -1171,11 +1157,12 @@ def process_def(
return new_s


def process_items(s: str, **kwargs) -> str:
def process_items(s: str, lang: str, **kwargs) -> str:
"""
Process itemize and enumerate.
:param s: Latex string code
:param lang: Language tag
:return: Processed items
"""
if not ('itemize' in s or 'enumerate' in s or 'tablenotes' in s):
Expand Down Expand Up @@ -1234,7 +1221,7 @@ def _are_item(e: str) -> bool:
t = _get_name(t)
if t == '':
continue
s = s[0:a] + _process_item(s[b:c].strip(), t) + s[d + 2:]
s = s[0:a] + remove_commands_param(_process_item(s[b:c].strip(), t), lang) + s[d + 2:]
conv = True
break
if not conv:
Expand Down
7 changes: 3 additions & 4 deletions pydetex/pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def simple(
"""
if len(s) == 0:
return s
pb = kwargs.get('progressbar', ProgressBar(steps=17 if replace_pydetex_tags else 16)) if show_progress else None
pb = kwargs.get('progressbar', ProgressBar(steps=16 if replace_pydetex_tags else 15)) if show_progress else None
s = '\n'.join(s.splitlines()) # Removes \r\n
s = par.process_inputs(s, pb=pb)
s = par.remove_comments(s, pb=pb)
Expand All @@ -52,8 +52,7 @@ def simple(
s = par.process_citeauthor(s, lang, pb=pb)
s = par.process_ref(s, pb=pb)
s = par.process_labels(s, pb=pb)
s = par.process_items(s, pb=pb)
s = par.process_quotes(s, pb=pb)
s = par.process_items(s, lang, pb=pb)
s = par.process_chars_equations(s, lang, single_only=True, pb=pb)
s = par.unicode_chars_equations(s, pb=pb)
s = par.remove_comments(s, pb=pb) # comments, replace tags, strip
Expand All @@ -79,7 +78,7 @@ def strict(
:param show_progress: Show progress bar
:return: String with no latex!
"""
pb = ProgressBar(steps=22) if show_progress else None
pb = ProgressBar(steps=21) if show_progress else None
if 'progressbar' not in kwargs.keys():
# noinspection PyTypeChecker
kwargs['progressbar'] = pb
Expand Down
29 changes: 16 additions & 13 deletions test/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,6 @@ def test_simple_replace(self) -> None:
self.assertEqual(par.simple_replace('This is a $x_0$ and \(x^2\)'), 'This is a $x₀$ and \(x²\)')
self.assertEqual(par.simple_replace('This is $\\alpha$'), 'This is $α$')

def test_process_quotes(self) -> None:
"""
Test quotes.
"""
self.assertEqual(par.process_quotes('This is \\quotes{a quoted} string'), 'This is "a quoted" string')
self.assertEqual(par.process_quotes('This is \\enquote{a quoted} string'), 'This is "a quoted" string')

def test_parse_inputs(self) -> None:
"""
Parse inputs.
Expand Down Expand Up @@ -331,6 +324,16 @@ def out(s_: str) -> str:
s = '\\lowercase{THIS is a Test}'
self.assertEqual(out(s), 'this is a test')

# Test quotes
s = '\quotes{a quoted}'
self.assertEqual(out(s), '"a quoted"')
s = '\enquote{a quoted}'
self.assertEqual(out(s), '"a quoted"')
s = '\quotes{\href{a}{link}}'
self.assertEqual(out(s), '"LINK: link"')
s = '\doublequotes{\href{a}{link}}'
self.assertEqual(out(s), '"LINK: link"')

def test_unicode_chars_equations(self) -> None:
"""
Test unicode char equations.
Expand Down Expand Up @@ -358,15 +361,15 @@ def test_process_items(self) -> None:
Test process items.
"""
s = '\\begin{itemize}\item a \item b\\begin{itemize}\item a \item b\end{itemize}\end{itemize}'
self.assertEqual(par.replace_pydetex_tags(par.process_items(s)),
self.assertEqual(par.replace_pydetex_tags(par.process_items(s, lang='en')),
'\n- a \n- b\n • a\n • b')

s = """\\begin{itemize}[font=\\bfseries]
\item As shown in Figure \\ref{fignumber}
\item Proposed
\end{itemize}"""
self.assertEqual(par.replace_pydetex_tags(par.process_items(s)),
'\n- As shown in Figure \\ref{fignumber}\n- Proposed')
self.assertEqual(par.replace_pydetex_tags(par.process_items(s, lang='en')),
'\n- As shown in Figure \n- Proposed')

s = """\\begin{enumerate}
\\item a
Expand Down Expand Up @@ -399,7 +402,7 @@ def test_process_items(self) -> None:
\\end{enumerate}
"""

t = par.replace_pydetex_tags(par.process_items(s))
t = par.replace_pydetex_tags(par.process_items(s, lang='en'))
self.assertEqual(
t, '\n1. a\n a) a\n b) b\n i. a\n ii. b\n iii. c\n'
' A) a\n B) b\n C) c\n I. a\n '
Expand All @@ -424,7 +427,7 @@ def test_process_items(self) -> None:
epic
"""
self.assertEqual(
par.replace_pydetex_tags(par.process_items(s)),
par.replace_pydetex_tags(par.process_items(s, lang='en')),
'\n \n1. b\n \n \n- a\n \n Note:'
' Res - Resolution in pixels (px).\n \n epic\n '
)
Expand Down Expand Up @@ -464,7 +467,7 @@ def test_process_items(self) -> None:
('nice', 461, 473, 482, 490, '', 0, -1))
)
self.assertEqual(
par.replace_pydetex_tags(par.process_items(s)).strip(),
par.replace_pydetex_tags(par.process_items(s, lang='en')).strip(),
'1. a\n \n1. a\n \n1. b\n \n1. a\n \n1. '
'b\n • c\n2. d\n \\begin{nice}\n \\end{nice}')

Expand Down
3 changes: 1 addition & 2 deletions test/test_pipelines.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def test_simple(self) -> None:
('data/example_simple_comments.txt', 'data/example_simple_comments_output.txt')
]
for f in example_files:
self.assertEqual(pip.simple(par._load_file_search(f[0])),
par._load_file_search(f[1]))
self.assertEqual(pip.simple(par._load_file_search(f[0])), par._load_file_search(f[1]))

def test_strict(self) -> None:
"""
Expand Down

0 comments on commit 4fadf3c

Please sign in to comment.