From 4d674531da66d668ff5b6292da849d07d315c60d Mon Sep 17 00:00:00 2001 From: Timo Richter Date: Sun, 7 Aug 2011 22:23:21 +0200 Subject: Implemented the language separator Cleaned up metabook.py convert now uses parameters. Try convert.py -h call_convert.sh calls convert.py as is was without parameters --- helpcontent2/wiki-to-help/convert.py | 133 ++++++++++++++++++++++------------- 1 file changed, 84 insertions(+), 49 deletions(-) (limited to 'helpcontent2/wiki-to-help/convert.py') diff --git a/helpcontent2/wiki-to-help/convert.py b/helpcontent2/wiki-to-help/convert.py index 1c92004e56..2bbc82e944 100755 --- a/helpcontent2/wiki-to-help/convert.py +++ b/helpcontent2/wiki-to-help/convert.py @@ -13,21 +13,49 @@ Microsoft HHC: http://go.microsoft.com/fwlink/?LinkId=14188 """ -import subprocess, tempfile, os, shutil +import subprocess, tempfile, os, shutil, argparse import mwlib_mods from hhc import HHC from mw import MW from metabook_translated import MetabookTranslated +from metabook_translated import LanguageSeparator scriptpath=os.path.dirname(os.path.realpath(__file__) ) class Main(object): - createChm = True # final - keepTmp = True # final - workingDir = "./test" # final + ''' Defines program parameters and returns them as a dictionary ''' + def parseArgs(self): + parser = argparse.ArgumentParser(description='Conversion from a mediawiki xml-dumpfile to helpfiles') + parser.add_argument("--startpage", metavar="Path", dest="startpage", default=None, type=str, help="Sets a HTML-file as the start page") + parser.add_argument("--keep", dest="keepTmp", default=False, action='store_true', help="Keeps temporary files in /tmp") + parser.add_argument("--only-en", dest="onlyEn", action='store_true', default=False, help="Convert only English articles") + parser.add_argument("--no-chm", dest="createChm", default=True, action='store_false', help="Avoids creation of a CHM file at the end") + parser.add_argument("input", type=str, help="XML input") + parser.add_argument("output", type=str, help="Directory for output") + + return parser.parse_args() + + def __init__(self): + args = self.parseArgs() + r = Converter( + keepTmp=args.keepTmp, + createChm=args.createChm, + source=args.input, + dest=args.output, + startpage=args.startpage, + onlyEn=args.onlyEn, + )() + exit(int(not r)) + + +class Converter(object): + createChm = None # + keepTmp = None # + #workingDir = "./test" # final #style=os.path.join(scriptpath,'xsl/htmlhelp/htmlhelp.xsl') # final style=os.path.join(scriptpath,'htmlhelp.xsl') # final + title="LibreOffice" # final tmp=None @@ -41,66 +69,73 @@ class Main(object): print cmd return (subprocess.Popen(cmd).wait() == 0) - def __init__(self): + def __init__(self,source,dest,onlyEn,keepTmp=False,createChm=True,startpage=None): + """ + @source XML-Dump-file + @dest Directory for output + @startpage Path to an html file + """ + self.createChm = createChm + self.keepTmp=keepTmp self.tmp = tempfile.mkdtemp() - - self.workingDir = os.path.abspath(self.workingDir) self.style = os.path.abspath(self.style) - self.hhc = HHC() - self.convert("test2.xml",self.workingDir) - + source = os.path.abspath(source) + dest = os.path.abspath(dest) + if startpage is not None: + startpage = os.path.abspath(startpage) + self.source=source + self.dest=dest + self.startpage=startpage + self.onlyEn = onlyEn + def createDir(self,path): try: os.mkdir(path) except OSError: pass - @staticmethod - def createMetabook(xmldump,output): # TODO: move to class Metabook + def __call__(self): """ - @xmldump String path - @output String path + Create the environment for conversion and call convert() + @return boolean Success """ - m = MetabookTranslated() - jsonStructFile = os.path.join(scriptpath,"metabook.json") - with open(jsonStructFile,"r") as f: - m.loadTemplate(f) - m(xmldump) - with open(output,"w") as f: - m.write(f) - - def convert(self,source,dest,startpage=None,title="LibreOffice"): + tmp = self.tmp + self.createDir(self.dest) + + shutil.copy(os.path.join(scriptpath,"nfo.json"),tmp) + metabook_template = os.path.join(scriptpath,"metabook.json") + ls = LanguageSeparator.fromFileToFiles(metabook_template,self.source,tmp) + MW.buildcdb(self.source,tmp) + + if self.onlyEn: + return self.convert("en",ls["en"]) + else: + + for lang, metabook in ls.iteritems(): + if not self.convert(lang,metabook): return False + + def convert(self,lang,metabook): """ - Create the converted files. - @source XML-Dump-file - @dest Directory for output - @startpage Path to an html file + Private. + This function executes the programs for the conversion. + @lang Language of book + @metabook Path to metabook-json-file """ tmp = self.tmp - self.createDir(dest) + docbookfile = os.path.join(tmp,"docbook_%s.xml"%lang) + chmDest = os.path.join(self.dest,lang+".chm") - shutil.copy(os.path.join(scriptpath,"nfo.json"),tmp) - #names = self.getArtNames(source) - metabook=os.path.join(tmp,"metabook.json") - self.createMetabook(source,metabook) - - MW.buildcdb(source,tmp) - #MW.render("--config=%s/wikiconf.txt"%(tmp), - # "-w","docbook","-o",tmp+"/docbook.xml",*names) MW.render("--config=%s/wikiconf.txt"%(tmp), - "-w","docbook","-o",tmp+"/docbook.xml","-m",metabook,"--title",title) - #and mwlib.apps.render - #self.ex(self.mwpath+"mw-buildcdb","--input",source,"--output",tmp) and \ - #self.ex( - # self.mwpath+"mw-render","--config=%s/wikiconf.txt"%(tmp), - # "-w","docbook","-o",tmp+"/docbook.xml",*names) \ - (shutil.copy(tmp+'/docbook.xml',dest) or True) \ - and self.ex("/usr/bin/xsltproc","--nonet","--novalid","-o",tmp+'/',self.style,tmp+'/docbook.xml') \ - and self.setStartpage(startpage) \ - and self.createChm \ - and (self.hhc(tmp) or True) \ - and (shutil.copy(os.path.join(tmp,'htmlhelp.chm'),dest) or True) + "-w","docbook","-o",docbookfile,"-m",metabook,"--title",self.title) + shutil.copy(docbookfile,self.dest) + if not self.ex("/usr/bin/xsltproc","--nonet","--novalid","-o",tmp+'/',self.style,docbookfile): return False + self.setStartpage(self.startpage) + if self.createChm: + print("Compiling chm...") + self.hhc(tmp) + shutil.copy(os.path.join(tmp,'htmlhelp.chm'),chmDest) + return True def setStartpage(self,startpage): """ @@ -110,7 +145,7 @@ class Main(object): """ if startpage is None: return True filename="index.html" - if not os.path.exist(startpage): return False + if not os.path.exists(startpage): return False os.remove(os.path.join(self.tmp,filename)) shutil.copy(startpage, os.path.join(self.tmp,filename)) return True -- cgit