LibreOffice Module filter (master) 1
js2hxx.py
Go to the documentation of this file.
1#!/usr/bin/env python
2#
3# This file is part of the LibreOffice project.
4#
5# This Source Code Form is subject to the terms of the Mozilla Public
6# License, v. 2.0. If a copy of the MPL was not distributed with this
7# file, You can obtain one at http://mozilla.org/MPL/2.0/.
8#
9# Software distributed under the License is distributed on an "AS IS" basis,
10# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11# for the specific language governing rights and limitations under the
12# License.
13#
14# Major Contributor(s):
15# Copyright (C) 2011 Marco Cecchetti <mrcekets@gmail.com>
16#
17# All Rights Reserved.
18#
19# For minor contributions see the git repository.
20
21import os, sys
22
23MAX_LINES = 150
24VARIABLE_NAME = 'aSVGScript'
25
27 return 'static const char %s%d[] =' % ( VARIABLE_NAME, n )
28
29script_name = os.path.basename( sys.argv[0] )
30infile_name = sys.argv[1]
31outfile_name = sys.argv[2]
32
33
34# collect input JavaScript file lines
35if( not os.path.isfile( infile_name ) ):
36 print ( '%s: error: file "%s" not found' % ( script_name, infile_name ) )
37 sys.exit( -1 )
38
39infile = open( infile_name, 'r' )
40in_lines = [line.rstrip() for line in infile.readlines()]
41infile.close()
42
43
44valid_lines=[]
45is_multiline_comment = False
46lineNumber = 0
47emptyLineCount = 0
48for line in in_lines:
49 lineNumber += 1
50 index = line.find('"')
51 if( index != -1 ):
52 print ( '%s: warning: processed file contains \'"\' at %d:%d' % ( script_name, lineNumber, index ) )
53
54 sline = line.strip()
55
56 # strip comment lines except multilines comments that begins with one '/' and exactly 5 '*'
57 if( is_multiline_comment and sline.endswith( '*/' ) ):
58 is_multiline_comment = False
59 continue
60
61 if( is_multiline_comment ):
62 continue
63
64 if( sline.startswith( '//' ) ):
65 continue
66
67 if( sline.startswith( '/*' ) and sline.endswith( '*/' ) ):
68 continue
69
70 if( ( sline.startswith( '/*' ) and not sline.startswith( '/*****' ) )
71 or sline.startswith( '/******' ) ):
72 is_multiline_comment = True
73 continue
74
75 # disable any debug printer
76 dline = line.replace( 'NAVDBG.on', 'NAVDBG.off' )
77 dline = dline.replace( 'ANIMDBG.on', 'ANIMDBG.off' )
78 dline = dline.replace( 'DebugPrinter.on', 'DebugPrinter.off' )
79
80 escaped_line = '%s' % dline
81 escaped_line = escaped_line.rstrip().lstrip()
82
83 # no more than 2 consecutive empty lines
84 if( escaped_line == '' ):
85 emptyLineCount += 1
86 else:
87 emptyLineCount = 0
88
89 if( emptyLineCount > 2 ):
90 continue
91
92 # append to some escape sequence another '\'
93 escaped_line = escaped_line.replace( '\\', '\\\\' )
94 escaped_line = escaped_line.replace( '\n', '\\n')
95 escaped_line = escaped_line.replace( '\t', '\\t' )
96
97 valid_lines.append( escaped_line )
98
99
100# compute the number of needed fragments that is of C constant strings
101total_valid_lines = len (valid_lines) + 2
102total_fragments = total_valid_lines / MAX_LINES
103if( ( total_valid_lines % MAX_LINES ) != 0 ):
104 total_fragments += 1
105
106
107
108out_lines = []
109out_lines.append( '' )
110out_lines.append( '#define N_SVGSCRIPT_FRAGMENTS %d' % total_fragments )
111out_lines.append( '' )
112out_lines.append( get_var_decl( 0 ) )
113out_lines.append( '"<![CDATA[\\n\\' )
114i = 2
115fragment = 0
116for line in valid_lines:
117 out_lines.append( line + '\\n\\' )
118 if( i == MAX_LINES ):
119 i = 0
120 fragment += 1
121 out_lines.append( '";' )
122 out_lines.append( '' )
123 out_lines.append( get_var_decl( fragment ) )
124 out_lines.append( '"\\' )
125 i += 1
126
127out_lines.append( ']]>";' )
128out_lines.append( '' )
129
130out_lines.append('static const char * g_SVGScripts[N_SVGSCRIPT_FRAGMENTS] = {')
131for j in range(0, fragment+1):
132 out_lines.append(" %s%d," % (VARIABLE_NAME, j))
133out_lines.append('};')
134
135outfile = open( outfile_name, 'w' )
136if( not os.path.isfile( outfile_name ) ):
137 print ( '%s: error: I cannot create file "%s"' % ( script_name, outfile_name ) )
138 sys.exit( -1 )
139
140
141# C++ header
142header_info = '/* !! This file is auto-generated, do not edit !! */'
143
144outfile.write( header_info +'\n\n' )
145
146for line in out_lines:
147 outfile.write( line + '\n' )
148
149outfile.close()
def get_var_decl(n)
Definition: js2hxx.py:26