-
Notifications
You must be signed in to change notification settings - Fork 1
/
do-convert-single-file
executable file
·310 lines (259 loc) · 12.6 KB
/
do-convert-single-file
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
#!/usr/bin/env python3
import chardet
import io
import json
import os
import pygments.formatter
import pygments.lexers
import pygments.lexers.c_cpp
import pygments.lexers.css
import pygments.lexers.data
import pygments.lexers.diff
import pygments.lexers.go
import pygments.lexers.javascript
import pygments.lexers.jvm
import pygments.lexers.python
import pygments.lexers.ruby
import pygments.lexers.rust
import pygments.lexers.shell
import pygments.lexers.special
import pygments.lexers.sql
from xml.sax.saxutils import escape
from reportlab.platypus import SimpleDocTemplate
from reportlab.platypus.xpreformatted import XPreformatted
from reportlab.lib.styles import ParagraphStyle
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
# ReportLab only supports 65,535 glyphs.
#
# TODO consider using an alternate library
pdfmetrics.registerFont(TTFont('NotoSansMono', '/app/NotoSansMono-Regular.ttf'))
class ReportLabFormatter(pygments.formatter.Formatter):
def __init__(self, **kwargs):
pygments.formatter.Formatter.__init__(self, **kwargs)
self._init_styles()
self._rl_style = ParagraphStyle(
name='Default',
fontName='NotoSansMono',
fontSize=9,
leading=13,
spaceBefore=0,
spaceAfter=0
)
def _init_styles(self):
# https://github.com/shkumagai/pygments-style-solarized/blob/develop/pygments_style_solarized/light.py
# minus italics and bold
from pygments.token import Keyword, Name, Comment, String, Error, Text, Number, Operator, Generic, Whitespace, Other, Literal, Punctuation
self._styles = {
Text: 'HexColor(0x657b83)', # base00 ; class: ''
Whitespace: 'HexColor(0xfdf6e3)', # base3 ; class: 'w'
Error: 'HexColor(0xdc322f)', # red ; class: 'err'
Other: 'HexColor(0x657b83)', # base00 ; class: 'x'
Comment: 'HexColor(0x93a1a1)', # base1 ; class: 'c'
Comment.Multiline: 'HexColor(0x93a1a1)', # base1 ; class: 'cm'
Comment.Preproc: 'HexColor(0x93a1a1)', # base1 ; class: 'cp'
Comment.Single: 'HexColor(0x93a1a1)', # base1 ; class: 'c1'
Comment.Special: 'HexColor(0x93a1a1)', # base1 ; class: 'cs'
Keyword: 'HexColor(0x859900)', # green ; class: 'k'
Keyword.Constant: 'HexColor(0x859900)', # green ; class: 'kc'
Keyword.Declaration: 'HexColor(0x859900)', # green ; class: 'kd'
Keyword.Namespace: 'HexColor(0xcb4b16)', # orange ; class: 'kn'
Keyword.Pseudo: 'HexColor(0xcb4b16)', # orange ; class: 'kp'
Keyword.Reserved: 'HexColor(0x859900)', # green ; class: 'kr'
Keyword.Type: 'HexColor(0x859900)', # green ; class: 'kt'
Operator: 'HexColor(0x657b83)', # base00 ; class: 'o'
Operator.Word: 'HexColor(0x859900)', # green ; class: 'ow'
Name: 'HexColor(0x586e75)', # base01 ; class: 'n'
Name.Attribute: 'HexColor(0x657b83)', # base00 ; class: 'na'
Name.Builtin: 'HexColor(0x268bd2)', # blue ; class: 'nb'
Name.Builtin.Pseudo: 'HexColor(0x268bd2)', # blue ; class: 'nb'
Name.Class: 'HexColor(0x268bd2)', # blue ; class: 'nc'
Name.Constant: 'HexColor(0xb58900)', # yellow ; class: 'no'
Name.Decorator: 'HexColor(0xcb4b16)', # orange ; class: 'nd'
Name.Entity: 'HexColor(0xcb4b16)', # orange ; class: 'ni'
Name.Exception: 'HexColor(0xcb4b16)', # orange ; class: 'ne'
Name.Function: 'HexColor(0x268bd2)', # blue ; class: 'nf'
Name.Property: 'HexColor(0x268bd2)', # blue ; class: 'py'
Name.Label: 'HexColor(0x657b83)', # base00 ; class: 'nc'
Name.Namespace: 'HexColor(0xb58900)', # yellow ; class: 'nn'
Name.Other: 'HexColor(0x657b83)', # base00 ; class: 'nx'
Name.Tag: 'HexColor(0x859900)', # green ; class: 'nt'
Name.Variable: 'HexColor(0xcb4b16)', # orange ; class: 'nv'
Name.Variable.Class: 'HexColor(0x268bd2)', # blue ; class: 'vc'
Name.Variable.Global: 'HexColor(0x268bd2)', # blue ; class: 'vg'
Name.Variable.Instance: 'HexColor(0x268bd2)', # blue ; class: 'vi'
Number: 'HexColor(0x2aa198)', # cyan ; class: 'm'
Number.Float: 'HexColor(0x2aa198)', # cyan ; class: 'mf'
Number.Hex: 'HexColor(0x2aa198)', # cyan ; class: 'mh'
Number.Integer: 'HexColor(0x2aa198)', # cyan ; class: 'mi'
Number.Integer.Long: 'HexColor(0x2aa198)', # cyan ; class: 'il'
Number.Oct: 'HexColor(0x2aa198)', # cyan ; class: 'mo'
Literal: 'HexColor(0x657b83)', # base00 ; class: 'l'
Literal.Date: 'HexColor(0x657b83)', # base00 ; class: 'ld'
Punctuation: 'HexColor(0x657b83)', # base00 ; class: 'p'
String: 'HexColor(0x2aa198)', # cyan ; class: 's'
String.Backtick: 'HexColor(0x2aa198)', # cyan ; class: 'sb'
String.Char: 'HexColor(0x2aa198)', # cyan ; class: 'sc'
String.Doc: 'HexColor(0x2aa198)', # cyan ; class: 'sd'
String.Double: 'HexColor(0x2aa198)', # cyan ; class: 's2'
String.Escape: 'HexColor(0xcb4b16)', # orange ; class: 'se'
String.Heredoc: 'HexColor(0x2aa198)', # cyan ; class: 'sh'
String.Interpol: 'HexColor(0xcb4b16)', # orange ; class: 'si'
String.Other: 'HexColor(0x2aa198)', # cyan ; class: 'sx'
String.Regex: 'HexColor(0x2aa198)', # cyan ; class: 'sr'
String.Single: 'HexColor(0x2aa198)', # cyan ; class: 's1'
String.Symbol: 'HexColor(0x2aa198)', # cyan ; class: 'ss'
Generic: 'HexColor(0x657b83)', # base00 ; class: 'g'
Generic.Deleted: 'HexColor(0x657b83)', # base00 ; class: 'gd'
Generic.Emph: 'HexColor(0x657b83)', # base00 ; class: 'ge'
Generic.Error: 'HexColor(0x657b83)', # base00 ; class: 'gr'
Generic.Heading: 'HexColor(0x657b83)', # base00 ; class: 'gh'
Generic.Inserted: 'HexColor(0x657b83)', # base00 ; class: 'gi'
Generic.Output: 'HexColor(0x657b83)', # base00 ; class: 'go'
Generic.Prompt: 'HexColor(0x657b83)', # base00 ; class: 'gp'
Generic.Strong: 'HexColor(0x657b83)', # base00 ; class: 'gs'
Generic.Subheading: 'HexColor(0x657b83)', # base00 ; class: 'gu'
Generic.Traceback: 'HexColor(0x657b83)', # base00 ; class: 'gt'
}
def _color_tokensource(self, tokensource):
"""Enumerate _colored_ tokens, instead of _typed_ tokens.
"""
lastval = ''
lastcolor = None
for ttype, token in tokensource:
while ttype not in self._styles:
ttype = ttype.parent
color = self._styles[ttype]
if token.isspace() or color == lastcolor:
lastval += token
else:
yield (color, lastval)
lastval = token
lastcolor = color
if lastval:
yield (color, lastval)
def format(self, tokensource, outfile):
"""Populates self.flowables with ReportLab-ready Flowables
Does not write anything to outfile.
"""
contents = []
frags = []
col = 0
max_col = 94
for color, value in self._color_tokensource(tokensource):
contents.append('<font color="%s">' % color)
lines = value.splitlines(True)
for line in lines:
while col + len(line) > max_col:
part = line[0 : max_col - col ]
contents.append(escape(part))
line = line[max_col - col :]
contents.append('\n')
col = 0
contents.append(escape(line))
if line.endswith('\n'):
col = 0
else:
col = col + len(line)
contents.append('</font>')
self.flowables = [ XPreformatted(''.join(contents), self._rl_style) ]
def read_input_blob():
"""Return the bytes in input.blob
"""
with open('input.blob', 'rb') as f:
return f.read()
def detect_bytes_charset(b):
"""Reads as many bytes of b as necessary to determine charset
"""
detector = chardet.UniversalDetector()
bio = io.BytesIO(b)
while True:
barray = bio.read(1024)
if len(barray) == 0:
break
detector.feed(barray)
if detector.done:
break
detector.close()
return detector.result.get('encoding') or 'utf-8'
def bytes_to_str(b):
"""Converts bytes to text by auto-detecting encoding.
"""
encoding = detect_bytes_charset(b)
return b.decode(encoding, 'replace')
def write_pdf(filename, flowables):
doc = SimpleDocTemplate(
'0.blob',
title=filename,
leftMargin=36.0,
rightMargin=36.0,
topMargin=36.0,
bottomMargin=36.0
)
doc.build(flowables)
def get_lexer_for_content_type(content_type):
"""Pick a lexer.
It's quicker than pygments' get_lexer_for_filename() because it supports
far fewer file types -- meaning less Python code to load.
"""
# We support at least the programming languages used by Overview itself
return {
'application/javascript': pygments.lexers.javascript.JavascriptLexer,
'application/json': pygments.lexers.data.JsonLexer,
'application/sql': pygments.lexers.sql.SqlLexer,
'application/vnd.coffeescript': pygments.lexers.javascript.CoffeeScriptLexer,
'application/x-ruby': pygments.lexers.ruby.RubyLexer,
'application/x-shellscript': pygments.lexers.shell.BashLexer,
'application/x-yaml': pygments.lexers.data.YamlLexer,
'text/css': pygments.lexers.css.CssLexer,
'text/rust': pygments.lexers.rust.RustLexer,
'text/x-chdr': pygments.lexers.c_cpp.CLexer,
'text/x-c++hdr': pygments.lexers.c_cpp.CppLexer,
'text/x-csrc': pygments.lexers.c_cpp.CLexer,
'text/x-c++src': pygments.lexers.c_cpp.CppLexer,
'text/x-gosrc': pygments.lexers.go.GoLexer,
'text/x-java': pygments.lexers.jvm.JavaLexer,
'text/x-kotlin': pygments.lexers.jvm.KotlinLexer,
'text/x-patch': pygments.lexers.diff.DiffLexer,
'text/x-python': pygments.lexers.python.PythonLexer,
'text/x-scala': pygments.lexers.jvm.ScalaLexer,
'text/x-scss': pygments.lexers.css.ScssLexer,
}.get(content_type, pygments.lexers.special.TextLexer)()
def calculate_text_flowables(text, content_type):
lexer = get_lexer_for_content_type(content_type)
formatter = ReportLabFormatter()
pygments.highlight(text, lexer, formatter)
return formatter.flowables
def write_text(text):
with open('0.txt', 'w') as f:
return f.write(text)
def main(input_json_text):
text = bytes_to_str(read_input_blob())
input_json = json.loads(input_json_text)
filename = input_json['filename']
content_type = input_json['contentType']
flowables = calculate_text_flowables(text, content_type)
write_pdf(filename, flowables)
write_text(text)
with open('0.json', 'w') as f:
json.dump({
'filename': filename,
'contentType': 'application/pdf',
'languageCode': input_json['languageCode'],
'wantOcr': False,
'wantSplitByPage': input_json['wantSplitByPage'],
'metadata': input_json['metadata']
}, f)
# exec() so signals get sent to pdftocairo
os.execl(
'/usr/bin/pdftocairo',
'/usr/bin/pdftocairo',
'-jpeg',
'-singlefile',
'-scale-to', '700',
'0.blob',
'0-thumbnail'
)
if __name__ == '__main__':
import sys
main(sys.argv[1])