Go to the documentation of this file.00001 import os
00002 import sys
00003 from string import Template
00004
00005 import wikidot.structure
00006 from wikidot.tree import WikiNode
00007
00008 qhp_template = \
00009 """<?xml version="1.0" encoding="UTF-8"?>
00010 <QtHelpProject version="1.0">
00011 <namespace>aseba.main</namespace>
00012 <virtualFolder>doc</virtualFolder>
00013
00014 ${filter_def}
00015
00016 ${filter_section}
00017
00018 </QtHelpProject>
00019 """
00020
00021 filter_def_template = \
00022 """ <customFilter name="${lang}">
00023 <filterAttribute>lang-${lang}</filterAttribute>
00024 </customFilter>
00025 """
00026
00027 filter_section_template = \
00028 """
00029 <filterSection>
00030 <filterAttribute>lang-${lang}</filterAttribute>
00031 <toc>
00032 ${sections}
00033 </toc>
00034
00035 <files>
00036 ${files}
00037 </files>
00038 </filterSection>
00039 """
00040
00041
00042 section_root_template = \
00043 """ <section title="${title}" ref = "${ref}">
00044 ${subsection}
00045 </section>
00046 """
00047 section_root = Template(section_root_template)
00048
00049
00050 section_leaf_template = \
00051 """ <section title="${title}" ref="${ref}"></section>
00052 """
00053 section_leaf = Template(section_leaf_template)
00054
00055
00056 file_element_template = \
00057 """ <file>${filename}</file>
00058 """
00059
00060
00061 item_template = """ <file>${filename}</file>\n"""
00062
00063 def __generate_sections__(node, output_directory):
00064 result = ""
00065 filename = os.path.join(output_directory, node.link)
00066 if len(node.children) == 0:
00067
00068 result += section_leaf.substitute(title=node.title, ref=filename)
00069 return result
00070
00071 subsections = ""
00072 for x in node.children:
00073 subsections += __generate_sections__(x, output_directory)
00074
00075 if node.link == '':
00076
00077 return subsections
00078 else:
00079 result += section_root.substitute(title=node.title, ref=filename, subsection=subsections)
00080 return result
00081
00082 def generate(source_directories, output_directory, languages, qhp_file):
00083 """
00084 directory / language should be lists
00085 """
00086
00087 print >> sys.stderr, "\nCreating {}, based on the content of {}...".format(qhp_file, source_directories)
00088
00089 qhp = Template(qhp_template)
00090 filterdefs = ""
00091 filterdef = Template(filter_def_template)
00092 filtersections = ""
00093 filtersection = Template(filter_section_template)
00094 for index, directory in enumerate(source_directories):
00095 lang = languages[index]
00096
00097 filterdefs += filterdef.substitute(lang=lang)
00098
00099 tree = wikidot.structure.get_structure(lang)
00100 sections = __generate_sections__(tree, output_directory)
00101
00102 files = [x for x in os.listdir(directory)]
00103 files.sort()
00104 filelist = ""
00105 fileitem = Template(file_element_template)
00106 for x in files:
00107 filename = os.path.join(output_directory, x)
00108
00109 filelist += fileitem.substitute(filename=filename)
00110
00111 filtersections += filtersection.substitute(lang=lang, files=filelist, sections=sections)
00112
00113 f = open(qhp_file, 'w')
00114 f.write(qhp.substitute(filter_def=filterdefs, filter_section=filtersections))
00115 f.close
00116 print >> sys.stderr, "Done."
00117