diff options
Diffstat (limited to 'po/htm2po.py')
-rwxr-xr-x | po/htm2po.py | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/po/htm2po.py b/po/htm2po.py new file mode 100755 index 0000000..683240e --- /dev/null +++ b/po/htm2po.py @@ -0,0 +1,97 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +import sys, re +from BeautifulSoup import BeautifulSoup + +help = """ +arg 1 : input file +arg 2 : the locale field which you wanna extract +arg 3 : output po file +""" + +DESIGN_ID = "Design ID" +dString = {} + +def findindexof( word, tds ): + for i, td in enumerate(tds): + try: + if td.contents[0]==word: + return i + except IndexError: + pass + return -1 + +if __name__=="__main__": + if len(sys.argv)>3: + in_file = sys.argv[1] + locale = sys.argv[2] + po_file = sys.argv[3] + else: + print help + sys.exit() + + print 'input file :', in_file + print 'locale :', locale + print 'po file :', po_file + + f = open( in_file, 'rt' ) + soup = BeautifulSoup( f.read() ) + f.close() + + for i, tr in enumerate(soup('tr')): + tds = tr('td') + if i==0: + iID = findindexof( DESIGN_ID, tds ) + iLocale = findindexof( locale, tds ) + print 'index of design id :',iID, ", index of",locale,":", iLocale + if iID<0 or iLocale<0: + print 'index failed' + sys.exit() + continue + + bPass = True; + for j,td in enumerate(tds): + try: + if j==iID: ID = td.contents[0] + elif j==iLocale: szLocale = td.contents[0] + except IndexError: + bPass = False; + pass + + if bPass: + dString[ID] = szLocale + #print i, '\t', ID, '\t' ,szLocale + + #modify po file + f = open( po_file, 'rt' ) + po = f.read().decode('utf-8') + f.close() + + for k, v in dString.items(): + #find key + msg = 'msgid "'+ k + '"' + sp = po.find( msg ) + + #if yes, replace + if sp>-1: + lines = po.split('\n') + for i, line in enumerate(lines): + if line.find( msg )>-1: + #replace next line + print 'replace :',k + v = v.replace('\r', '') #remove linefeed + v = v.replace('\n ', '\\n') #remove carriage return + lines[i+1] = 'msgstr "' + v + '"' + po = "\n".join( lines ) + + #if no, append + #if sp<0: + # print 'append', k + # po += 'msgid "' + k + '"\n' + # po += 'msgstr "' + v + '"\n\n' + + newfile = po_file+'.new' + f = open( newfile , 'wt' ) + f.write( po.encode('utf-8') ) + f. close() + print newfile, 'is created' |