File: utils.py

package info (click to toggle)
python-pylatex 1.4.2%2Bds-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,044 kB
  • sloc: python: 3,810; sh: 209; makefile: 169; xml: 12
file content (354 lines) | stat: -rw-r--r-- 8,539 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# -*- coding: utf-8 -*-
"""
This module implements some simple utility functions.

..  :copyright: (c) 2014 by Jelte Fennema.
    :license: MIT, see License for more details.
"""

import os.path
import shutil
import tempfile

import pylatex.base_classes

_latex_special_chars = {
    "&": r"\&",
    "%": r"\%",
    "$": r"\$",
    "#": r"\#",
    "_": r"\_",
    "{": r"\{",
    "}": r"\}",
    "~": r"\textasciitilde{}",
    "^": r"\^{}",
    "\\": r"\textbackslash{}",
    "\n": "\\newline%\n",
    "-": r"{-}",
    "\xA0": "~",  # Non-breaking space
    "[": r"{[}",
    "]": r"{]}",
}

_tmp_path = None


def _is_iterable(element):
    return hasattr(element, "__iter__") and not isinstance(element, str)


class NoEscape(str):
    """
    A simple string class that is not escaped.

    When a `.NoEscape` string is added to another `.NoEscape` string it will
    produce a `.NoEscape` string. If it is added to normal string it will
    produce a normal string.

    Args
    ----
    string: str
        The content of the `NoEscape` string.
    """

    def __repr__(self):
        return "%s(%s)" % (self.__class__.__name__, self)

    def __add__(self, right):
        s = super().__add__(right)
        if isinstance(right, NoEscape):
            return NoEscape(s)
        return s


def escape_latex(s):
    r"""Escape characters that are special in latex.

    Args
    ----
    s : `str`, `NoEscape` or anything that can be converted to string
        The string to be escaped. If this is not a string, it will be converted
        to a string using `str`. If it is a `NoEscape` string, it will pass
        through unchanged.

    Returns
    -------
    NoEscape
        The string, with special characters in latex escaped.

    Examples
    --------
    >>> escape_latex("Total cost: $30,000")
    'Total cost: \$30,000'
    >>> escape_latex("Issue #5 occurs in 30% of all cases")
    'Issue \#5 occurs in 30\% of all cases'
    >>> print(escape_latex("Total cost: $30,000"))

    References
    ----------
        * http://tex.stackexchange.com/a/34586/43228
        * http://stackoverflow.com/a/16264094/2570866
    """

    if isinstance(s, NoEscape):
        return s

    return NoEscape("".join(_latex_special_chars.get(c, c) for c in str(s)))


def fix_filename(path):
    r"""Fix filenames for use in LaTeX.

    Latex has problems if there are one or more points in the filename, thus
    'abc.def.jpg' will be changed to '{abc.def}.jpg'

    Windows gets angry about the curly braces that resolve the above issue on
    linux Latex distributions. MikTeX however, has no qualms about multiple
    dots in the filename so the behavior is different for posix vs nt when the
    length of file_parts is greater than two.

    Args
    ----
    filename : str
        The file name to be changed.

    Returns
    -------
    str
        The new filename.

    Examples
    --------
    >>> fix_filename("foo.bar.pdf")
    '{foo.bar}.pdf'
    >>> fix_filename("/etc/local/foo.bar.pdf")
    '/etc/local/{foo.bar}.pdf'
    >>> fix_filename("/etc/local/foo.bar.baz/document.pdf")
    '/etc/local/foo.bar.baz/document.pdf'
    >>> fix_filename("/etc/local/foo.bar.baz/foo~1/document.pdf")
    '\detokenize{/etc/local/foo.bar.baz/foo~1/document.pdf}'
    """

    path_parts = path.split("/" if os.name == "posix" else "\\")
    dir_parts = path_parts[:-1]

    filename = path_parts[-1]
    file_parts = filename.split(".")

    if os.name == "posix" and len(file_parts) > 2:
        filename = "{" + ".".join(file_parts[0:-1]) + "}." + file_parts[-1]

    dir_parts.append(filename)
    fixed_path = "/".join(dir_parts)

    if "~" in fixed_path:
        fixed_path = r"\detokenize{" + fixed_path + "}"

    return fixed_path


def dumps_list(l, *, escape=True, token="%\n", mapper=None, as_content=True):
    r"""Try to generate a LaTeX string of a list that can contain anything.

    Args
    ----
    l : list
        A list of objects to be converted into a single string.
    escape : bool
        Whether to escape special LaTeX characters in converted text.
    token : str
        The token (default is a newline) to separate objects in the list.
    mapper: callable or `list`
        A function, class or a list of functions/classes that should be called
        on all entries of the list after converting them to a string, for
        instance `~.bold` or `~.MediumText`.
    as_content: bool
        Indicates whether the items in the list should be dumped using
        `~.LatexObject.dumps_as_content`

    Returns
    -------
    NoEscape
        A single LaTeX string.

    Examples
    --------
    >>> dumps_list([r"\textbf{Test}", r"\nth{4}"])
    '\\textbf{Test}%\n\\nth{4}'
    >>> print(dumps_list([r"\textbf{Test}", r"\nth{4}"]))
    \textbf{Test}
    \nth{4}
    >>> print(pylatex.utils.dumps_list(["There are", 4, "lights!"]))
    There are
    4
    lights!
    >>> print(dumps_list(["$100%", "True"], escape=True))
    \$100\%
    True
    """
    strings = (
        _latex_item_to_string(i, escape=escape, as_content=as_content) for i in l
    )

    if mapper is not None:
        if not isinstance(mapper, list):
            mapper = [mapper]

        for m in mapper:
            strings = [m(s) for s in strings]
        strings = [_latex_item_to_string(s) for s in strings]

    return NoEscape(token.join(strings))


def _latex_item_to_string(item, *, escape=False, as_content=False):
    """Use the render method when possible, otherwise uses str.

    Args
    ----
    item: object
        An object that needs to be converted to a string
    escape: bool
        Flag that indicates if escaping is needed
    as_content: bool
        Indicates whether the item should be dumped using
        `~.LatexObject.dumps_as_content`

    Returns
    -------
    NoEscape
        Latex
    """

    if isinstance(item, pylatex.base_classes.LatexObject):
        if as_content:
            return item.dumps_as_content()
        else:
            return item.dumps()
    elif not isinstance(item, str):
        item = str(item)

    if escape:
        item = escape_latex(item)

    return item


def bold(s, *, escape=True):
    r"""Make a string appear bold in LaTeX formatting.

    bold() wraps a given string in the LaTeX command \textbf{}.

    Args
    ----
    s : str
        The string to be formatted.
    escape: bool
        If true the bold text will be escaped

    Returns
    -------
    NoEscape
        The formatted string.

    Examples
    --------
    >>> bold("hello")
    '\\textbf{hello}'
    >>> print(bold("hello"))
    \textbf{hello}
    """

    if escape:
        s = escape_latex(s)

    return NoEscape(r"\textbf{" + s + "}")


def italic(s, *, escape=True):
    r"""Make a string appear italicized in LaTeX formatting.

    italic() wraps a given string in the LaTeX command \textit{}.

    Args
    ----
    s : str
        The string to be formatted.
    escape: bool
        If true the italic text will be escaped

    Returns
    -------
    NoEscape
        The formatted string.

    Examples
    --------
    >>> italic("hello")
    '\\textit{hello}'
    >>> print(italic("hello"))
    \textit{hello}
    """
    if escape:
        s = escape_latex(s)

    return NoEscape(r"\textit{" + s + "}")


def verbatim(s, *, delimiter="|"):
    r"""Make the string verbatim.

    Wraps the given string in a \verb LaTeX command.

    Args
    ----
    s : str
        The string to be formatted.
    delimiter : str
        How to designate the verbatim text (default is a pipe | )

    Returns
    -------
    NoEscape
        The formatted string.

    Examples
    --------
    >>> verbatim(r"\renewcommand{}")
    '\\verb|\\renewcommand{}|'
    >>> print(verbatim(r"\renewcommand{}"))
    \verb|\renewcommand{}|
    >>> print(verbatim('pi|pe', '!'))
    \verb!pi|pe!
    """

    return NoEscape(r"\verb" + delimiter + s + delimiter)


def make_temp_dir():
    """Create a temporary directory if it doesn't exist.

    Returns
    -------
    str
        The absolute filepath to the created temporary directory.

    Examples
    --------
    >>> make_temp_dir()
    '/var/folders/g9/ct5f3_r52c37rbls5_9nc_qc0000gn/T/pylatex'
    """

    global _tmp_path
    if not _tmp_path:
        _tmp_path = tempfile.mkdtemp(prefix="pylatex-tmp.")
    return _tmp_path


def rm_temp_dir():
    """Remove the temporary directory specified in ``_tmp_path``."""

    global _tmp_path
    if _tmp_path:
        shutil.rmtree(_tmp_path)
        _tmp_path = None