summaryrefslogtreecommitdiff
path: root/helpcontent2/wiki-to-help
diff options
context:
space:
mode:
authorTimo Richter <timo@iera.de>2011-08-07 22:23:21 +0200
committerJan Holesovsky <kendy@suse.cz>2011-08-26 13:46:41 +0200
commit4d674531da66d668ff5b6292da849d07d315c60d (patch)
treed114d757fdb28e75059241b78e02c4e4ec6a645a /helpcontent2/wiki-to-help
parent4125fb5b27501a1481bf6746d4141d45896a3364 (diff)
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
Diffstat (limited to 'helpcontent2/wiki-to-help')
-rwxr-xr-xhelpcontent2/wiki-to-help/call_convert.sh3
-rwxr-xr-xhelpcontent2/wiki-to-help/convert.py133
-rw-r--r--helpcontent2/wiki-to-help/htmlhelp.xsl3
-rw-r--r--helpcontent2/wiki-to-help/metabook.py75
-rw-r--r--helpcontent2/wiki-to-help/metabook_translated.py64
5 files changed, 208 insertions, 70 deletions
diff --git a/helpcontent2/wiki-to-help/call_convert.sh b/helpcontent2/wiki-to-help/call_convert.sh
new file mode 100755
index 0000000000..c3ff714fcf
--- /dev/null
+++ b/helpcontent2/wiki-to-help/call_convert.sh
@@ -0,0 +1,3 @@
+#!/bin/bash
+./convert.py test2.xml test/
+
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
diff --git a/helpcontent2/wiki-to-help/htmlhelp.xsl b/helpcontent2/wiki-to-help/htmlhelp.xsl
index ba7ce7357f..93d9712038 100644
--- a/helpcontent2/wiki-to-help/htmlhelp.xsl
+++ b/helpcontent2/wiki-to-help/htmlhelp.xsl
@@ -17,8 +17,7 @@
******************************************************************** -->
<xsl:import href="xsl/html/chunk.xsl"/>
-<xsl:param name="htmlhelp.use.hhk" select="0"></xsl:param>
+<xsl:param name="htmlhelp.use.hhk" select="1"></xsl:param>
<xsl:include href="xsl/htmlhelp/htmlhelp-common.xsl"/>
-<xsl:param name="generate.index" select="1"/>
</xsl:stylesheet>
diff --git a/helpcontent2/wiki-to-help/metabook.py b/helpcontent2/wiki-to-help/metabook.py
index 575b7e2fb9..c6f05008cd 100644
--- a/helpcontent2/wiki-to-help/metabook.py
+++ b/helpcontent2/wiki-to-help/metabook.py
@@ -23,25 +23,36 @@ class Article(object):
class Metabook(object):
"""
I am your metabook and wish you a pleasant evening.
+ Sequence of usage:
+ m = Metabook()
+ m.loadTemplate(...)
+ m.loadArticles(xml input)
+ m.createBook()
+ m.write(output)
+ If template, in- and output are files, use fromFileToFile()
"""
ArticleClass = Article # final
artTags = ["title"] # final
m = {} # Dict metabook
- source = "" # String input file, xmldump
- dest = "" # FileObject destination of json metabook
+ template = None
+ items = []
+ #source = "" # String input file, xmldump
+ #dest = "" # FileObject destination of json metabook
def getClone(self):
m = Metabook()
- m.m = self.m.copy()
+ m.template = self.template # No copy() neccessary here
+ m.ArticleClass = self.ArticleClass
+ m.artTags = self.artTags
+ #m.m = self.m.copy()
#m.dest = self.dest
- #m.source = self.source
return m
def getArtTags(self,filename,tagnames):
"""
Get Article Tags
- Reads all title tags from an xml file and returns a list of all titles.
+ Reads all specified tags from an xml file and returns a list of all tags.
@filename XML-file
@tagnames List of String Tagnames
@return List of Dict<String Tagname, String Value>
@@ -80,32 +91,64 @@ class Metabook(object):
Loads an existing json file at the beginning
@jsonStruct File object
"""
- self.m = json.load(jsonStruct)
+ self.template = json.load(jsonStruct)
#self.m = self.load_data(source)
- def setArticles(self):
- pages = self.getArtTags(self.source,self.artTags)
+ def loadArticles(self,source):
+ """
+ Loads the articles and saves them as objects to self.items
+ """
+ pages = self.getArtTags(source,self.artTags)
+ self.items = [self.ArticleClass(page) for page in pages]
+ """return
items=[]
for page in pages:
- #item=self.itemTag.copy()
- #item["title"] = name
item = self.ArticleClass(page)
if item.getInclude():
items.append(item.toDict())
self.m["items"] = items
+ """
+
+ def createBook(self):
+ """
+ Convert all article objects to dicts and merge them with the template.
+ The result is saved to self.m
+ """
+ if self.template is None:
+ self.m = []
+ else:
+ self.m = self.template.copy()
+ self.m["items"] = []
+ for item in self.items:
+ if item.getInclude():
+ self.m["items"].append(item.toDict())
def __call__(self,source):
"""
- Creates a metabook for @source and saves it to @dest
+ Creates a metabook for @source and writes it to self.m. To continue,
+ use write()
@source xml-dump
- @dest File object as destination of json-file
"""
- self.source = source
- #self.dest = dest
- #self.loadStructure()
- self.setArticles()
+ self.loadArticles(source)
+ self.createBook()
def write(self,dest):
json.dump(self.m,dest)
+ def fromFileToFile(jsonStructFile,xmldump,output):
+ """
+ Creates a Metabook from a file and writes it to a file.
+ Short cut Function. This loads a metabook template file, creates the
+ metabook content from @xmldump and writes the book to @output.
+ @jsonStructFile String path to Metabook template
+ @xmldump String path
+ @output String path
+ """
+ #m = MetabookTranslated()
+ with open(jsonStructFile,"r") as f:
+ self.loadTemplate(f)
+ self.__call__(xmldump)
+ with open(output,"w") as f:
+ self.write(f)
+
diff --git a/helpcontent2/wiki-to-help/metabook_translated.py b/helpcontent2/wiki-to-help/metabook_translated.py
index 07364dd0d4..e9321c2fa5 100644
--- a/helpcontent2/wiki-to-help/metabook_translated.py
+++ b/helpcontent2/wiki-to-help/metabook_translated.py
@@ -1,5 +1,5 @@
import metabook
-import re
+import re, os
class ArticleTranslated(metabook.Article):
lang = "en" # default language code
@@ -47,9 +47,67 @@ class MetabookTranslated(metabook.Metabook):
ArticleClass=ArticleTranslated
artTags = ["title","comment"]
- def splitByLanguage(self):
+class LanguageSeparator(object):
+ """
+ A translated metabook is a metabook where all titles are in the destination
+ language.
+ This class splits a translated metabook into many books with homogenous languages.
+ """
+ books={} # Dict<Str lang, Metabook>
+ sortedItems={} # Dict<Str lang, List<TranslatedArticle>>
+ items=[] # List<TranslatedArticle>
+
+ def __init__(self, book):
+ self.book = book
+ self.items = book.items
+
+ def splitItemsByLanguage(self):
"""
@return List of Metabook
"""
- pass
+ sortedItems={}
+ for item in self.items:
+ if item.lang in sortedItems.keys():
+ sortedItems[item.lang].append(item)
+ else:
+ sortedItems[item.lang] = [item]
+ self.sortedItems = sortedItems
+ #return sortedItems
+
+ def createBooksByLanguage(self):
+ for lang, items in self.sortedItems.iteritems():
+ m = self.book.getClone()
+ m.items = items
+ m.lang = lang
+ self.books[lang] = m
+
+ @staticmethod
+ def fromFileToFiles(jsonStructFile,xmldump,output):
+ """
+ Creates a Metabook from a file and writes it to one file per language.
+ Short cut Function. This loads a metabook template file, creates the
+ metabook content from @xmldump and writes the book to @output.
+ @jsonStructFile String path to Metabook template
+ @xmldump String path
+ @output String path to output directory
+ @return Dict<String lang, String output>
+ """
+ m = MetabookTranslated()
+ with open(jsonStructFile,"r") as f:
+ m.loadTemplate(f)
+
+ m.loadArticles(xmldump)
+ ls = LanguageSeparator(m)
+ ls.splitItemsByLanguage()
+ ls.createBooksByLanguage()
+
+ pathlist={}
+
+ for lang, book in ls.books.iteritems():
+ book.createBook()
+ dest = os.path.join(output,"metabook_%s.json" % lang)
+ pathlist[lang] = dest
+ with open(dest,"w") as f:
+ book.write(f)
+ return pathlist