Debian Codegraph @

tools/utility/unicode2html.py

raw
1 #!/usr/bin/env python
2 from __future__ import print_function
3 from sys import argv, exit, stdout, stderr
4 import codecs
5 __license__ = """
6 This file is part of GNU FreeFont.
7
8 GNU FreeFont is free software: you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free Software
10 Foundation, either version 3 of the License, or (at your option) any later
11 version.
12
13 GNU FreeFont is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along with
18 GNU FreeFont. If not, see <http://www.gnu.org/licenses/>.
19 """
20 __author__ = "Stevan White"
21 __email__ = "[email protected]"
22 __copyright__ = "Copyright 2013, 2017, 2018 Stevan White"
23 __date__ = "$Date: 2018-06-03 22:17:08 +0200 (So, 03. Jun 2018) $"
24 __version__ = "$Revision: 3918 $"
25
26 __doc__ = """
27 Prints to standard output a stream of HTML (SGML) entity strings
28 corresponding to Unicode in the input file.
29
30 Purpose is to see clearly what characters are in a string of Unicode,
31 and to put them into the text of an HTML file in that clearly readable form.
32
33 This is a hack.
34 """
35
36 def explain_error_and_quit( e = False ):
37 if e:
38 print( 'Error:', e, file=sterr )
39 print( "Usage:", file=stderr )
40 print( " unicode2html filename", file=stderr )
41 print( " where filename is the name of a UTF8-encoded text file",
42 file=stderr )
43 exit( 1 )
44
45 def formatted_hex_value( n ):
46 return '%s%0.4x%s' %( "&#x", n, ";" )
47
48 def print_HTML_versions_of_utf8( infile ):
49 for line in infile:
50 for char in line.strip():
51 stdout.write( formatted_hex_value( ord( char ) ) )
52 stdout.write( '\n' )
53
54 if len( argv ) == 2:
55 try:
56 fileName = argv[1]
57 f = codecs.open( fileName, 'r', encoding='utf_8' )
58 print_HTML_versions_of_utf8( f )
59 except ValueError as e:
60 explain_error_and_quit( e )
61 else:
62 explain_error_and_quit()