Welcome to mirror list, hosted at ThFree Co, Russian Federation.

util.py « latex - dev.gajim.org/gajim/gajim-plugins.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 702cc629e69c112e9c3a0ac11ba10da1a16e5885 (plain)
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
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.

import logging
import os
from subprocess import Popen
from subprocess import PIPE

from gajim.plugins.plugins_i18n import _

log = logging.getLogger('gajim.p.latex')

BLACKLIST = [
    '\def',
    '\\let',
    '\\futurelet',
    '\\newcommand',
    '\\renewcomment',
    '\\else',
    '\\fi',
    '\\write',
    '\\input',
    '\\include',
    '\\chardef',
    '\\catcode',
    '\\makeatletter',
    '\\noexpand',
    '\\toksdef',
    '\\every',
    '\\errhelp',
    '\\errorstopmode',
    '\\scrollmode',
    '\\nonstopmode',
    '\\batchmode',
    '\\read',
    '\\csname',
    '\\newhelp',
    '\\relax',
    '\\afterground',
    '\\afterassignment',
    '\\expandafter',
    '\\noexpand',
    '\\special',
    '\\command',
    '\\loop',
    '\\repeat',
    '\\toks',
    '\\output',
    '\\line',
    '\\mathcode',
    '\\name',
    '\\item',
    '\\section',
    '\\mbox',
    '\\DeclareRobustCommand',
    '\\[',
    '\\]',
]


def try_run(argv, directory):
    try:
        proc = popen_nt_friendly(argv, directory)
        out = proc.communicate()[0]
        log.info(out)
        return proc.wait()
    except Exception as err:
        return _('Error executing "%(command)s": %(error)s') % {
            'command': " ".join(argv),
            'error': str(err)}


def popen_nt_friendly(command, directory):
    if os.name == 'nt':
        # CREATE_NO_WINDOW
        return Popen(command, creationflags=0x08000000, cwd=directory,
                     stdout=PIPE)
    return Popen(command, cwd=directory, stdout=PIPE)


def write_latex(filename, string):
    texstr = _get_latex_template(string)

    file_ = open(filename, 'w+')
    file_.write(texstr)
    file_.flush()
    file_.close()


def _get_latex_template(code):
    template = '''
        \\documentclass[12pt]{article}
        \\usepackage[dvips]{graphicx}
        \\usepackage{amsmath}
        \\usepackage{amssymb}
        \\pagestyle{empty}
        \\begin{document}
        \\begin{large}
        \\begin{gather*}
        %s
        \\end{gather*}
        \\end{large}
        \\end{document}''' % (code)
    return template