File size: 5,413 Bytes
2c67080
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding: utf-8
# [Pix2Text](https://github.com/breezedeus/pix2text): an Open-Source Alternative to Mathpix.
# Copyright (C) 2022-2024, [Breezedeus](https://www.breezedeus.com).

COLOR_MAPPING = {
    'general': '#009933',
    'english': '#3399ff',
    'formula': '#ff8000',
    'hybrid': '#009999',
    'text': '#3399ff',
    'isolated': '#ff8000',
    'text-embed': '#009999',
}

def render_html(newest_fp, image_type, text, out_html_fp):
    html_str = """
<!DOCTYPE html>
<html>
  <head>
    <link
      rel="stylesheet"
      href="https://cindyjs.org/dist/v0.8/katex/katex.min.css"
    />
    <script
      type="text/javascript"
      src="https://cindyjs.org/dist/v0.8/katex/katex.min.js"
    ></script>
    <script
      type="text/javascript"
      src="https://cindyjs.org/dist/v0.8/webfont.js"
    ></script>
    <style>
      body {
        /* background-color: rgb(154, 183, 249); */
      }
      #latex {
        min-height: auto;
        margin-left: auto;
        margin-right: auto;
        width: 760px;
      }

      #textarea {
        margin-left: auto;
        margin-right: auto;
        display: block;
      padding: 16px;
        /* height: 200px; */
      }

      body {
        display: flex;
        flex-direction: column;
        align-items: center;
      }

      .img-container {
        max-width: 760px;
      }
      .img-container img {
        max-width: 100%
      }
            .row {
        display: flex;
        gap: 1em;
        width: 760px;
      }
      .row textarea {
        flex: 1;
      }
      .row .col {
        width: 80px;
        display: flex;
        flex-direction: column;
        gap: 0.5em;
      }

      .container {
        width: 760px;
        position: relative;
        display: flex;
        flex-direction: column;
        align-items: center;
      }
      .container .refresh {
        position: absolute;
        right: 0;
        top: 0;
        padding: 4px 8px;
      }
      .btn {
        font-size: 16px;
        padding: 4px 2px;
        border: none;
        border-radius: 4px;
        font-weight: bolder;
        cursor: pointer;
        color: rgb(62, 59, 59);
        background-color: #b3b3cc
      }
      .img-type {
        font-size: 16px;
        padding: 4px 6px;
        border: none;
        border-radius: 4px;
        font-weight: bolder;
        color: #ffffff
      }
      .cp {
        background-color: #f07878;
      }
      .cpd {
        background-color:   #a7d797;
      }
      .cpdd {
        background-color: #74a7f9;
      }
    </style>
  </head>

  <body class="with-footer">
    <div class="container">
    <h1 align="center"><a href="https://github.com/breezedeus/pix2text">Pix2Text</a>: a free tool like Mathpix</h1>
    <h2 align="center">Screenshot</h2>
    <div class="img-container">
    """
    html_str += fr'<img src="{newest_fp}" />' + '\n </div>'
    html_str += """
    <button class="refresh btn" onClick="document.location.reload()">Refresh</button>
    </div>
    <hr />

    <h2 align="center">Results</h2>
    """

    html_str += r'<strong>Image Type: </strong>' \
                fr'<div class="img-type" style="background:{COLOR_MAPPING[image_type]}"> ' \
                fr'{image_type} </div>' + '\n'

    if image_type in ('formula', 'hybrid'):
        html_str += '<div id="latex"></div>'

    html_str += """

    <hr>

    <div class="row">

    """

    html_str += '\n<textarea id="textarea" rows="10">' + fr"{text}" + '</textarea>'

    html_str += """
          <div class="col">
        <button class="btn" type="button" onclick="copyTex()">Copy</button>
        <button class="btn" type="button" onclick="copyTexD()">$Copy$</button>
        <button class="btn" type="button" onclick="copyTexDD()">$$Copy$$</button>
      </div>
    </div>


    <script type="text/javascript">
      const textarea = document.querySelector("#textarea");
      const render = () => {
        var elt = document.createElement("div");
        elt.id = "latex";
        try {
          katex.render(textarea.value, elt, { displayMode: "display" });
        } catch (err) {
          console.error(err);
        }
        document.body.replaceChild(elt, document.querySelector("#latex"));
      };

      textarea.onblur = render;
      render();
    </script>

    <script type="text/javascript">
      function copy(text) {
        navigator.permissions
          .query({ name: "clipboard-write" })
          .then((result) => {
            if (result.state == "granted" || result.state == "prompt") {
              navigator.clipboard.writeText(text).then(() => {
                /* do nothing */
              });
            }
          });
      }
      function copyTex() {
        const texText = document.querySelector("#textarea").textContent;
        copy(texText);
      }
      function copyTexD() {
        const texText = document.querySelector("#textarea").textContent;
        copy(`$${texText}$`);
      }
      function copyTexDD() {
        const texText = document.querySelector("#textarea").textContent;
        copy(`$$${texText}$$`);
      }
    </script>

    <script>
      (async function callApi() {
        const resp = await fetch('/api/ocr')
        const data = await resp.json()
        console.log(data)
      })()
    </script>
  </body>
</html>
    """

    with open(out_html_fp, 'w', encoding='utf-8') as f:
        f.writelines(html_str)