summaryrefslogtreecommitdiff
path: root/helpcontent2/wiki-to-help/convert.py
blob: 0298391364795c1941b1e8a55c1d83cd26ca6fb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
"""
Convert an XML-Dump to platformspecific help files.
Copyright 2011 Timo Richter
Licensed under GNU GPLv3

This program depends on:
mwlib
python
python-lxml
xsltproc
Microsoft HHC: http://go.microsoft.com/fwlink/?LinkId=14188


"""

import xml.dom.minidom as minidom
import subprocess, tempfile, os, shutil

class Wine(object):
    #driveletter="j:" #final

    def __init__(self,workingDir,driveletter):
        """ Setup the wine environment. Granting access so that wine is able to output files to @workingDir.
        @workingDir will be accessable via @driveletter
        E.g. Wine("/tmp/dir","j:") """
        homedir = os.path.expanduser('~')
        wineprefix=os.path.join(homedir,".wine")
        drive=os.path.join(wineprefix,"dosdevices",driveletter)
        if os.path.lexists(drive):
            self.driveBak = drive+".bak"
            shutil.move(drive,self.driveBak)
        os.symlink(workingDir,drive)
        self.drive = drive
        #self.driveBak = driveBak

    def ex(self,*cmd):
        """ execute something with wine """
        cmd = [elem for elem in cmd]
        cmd = ["/usr/bin/wine"]+cmd
        r= (subprocess.Popen(cmd).wait())
        return r

    def __call__(self,*cmd):
        return self.ex(*cmd)

    def __del__(self):
        os.remove(self.drive)
        if hasattr(self,'driveBak'):
            shutil.move(self.driveBak,self.drive)




class Main(object):
    workingDir = "./test" # final
    mwpath='/usr/local/bin/' # final
    style='/usr/share/xml/docbook/stylesheet/docbook-xsl/htmlhelp/htmlhelp.xsl' # final

    tmp=None

    def ex(self,*cmd):
        """
        Execute a program
        @cmd Command, args
        @return boolean True if succeed
        """
        cmd = [elem for elem in cmd]
        print cmd
        return (subprocess.Popen(cmd).wait() == 0)

    def __init__(self):
        self.tmp = tempfile.mkdtemp()

        self.workingDir = os.path.abspath(self.workingDir)
        self.style = os.path.abspath(self.style)

        self.wine = Wine(self.tmp,"j:")
        self.convert("test2.xml",self.workingDir)

    def convert(self,source,dest):
        """
        Create the converted files.
        @source XML-Dump-file
        @dest Directory for output
        """
        tmp = self.tmp
        try:
            os.mkdir(dest)
        except OSError:
            pass

        names = self.getArtNames(source)
        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) \
        and (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.wine("c:\\htmlhelp\\hhc.exe","j:\\htmlhelp.hhp") or True) \
        and (shutil.copy(tmp+'/htmlhelp.chm',dest) or True)

    def __del__(self):
        shutil.rmtree(self.tmp) # remove temp files
        pass

    def getArtNames(self,filename):
        """
        Get Article Names
        Reads all title tags from an xml file and returns a list of all titles.
        @filename XML-file
        @return List of Strings
        """
        dom=minidom.parse(filename)
        elements=dom.getElementsByTagName("title")
        names=[]
        for element in elements:
            name=element.childNodes[0].data
            names.append(name)
        return names

if __name__ == '__main__':
    Main()