# -*- coding: utf-8 -*- """ *************************************************************************** Help2Html.py --------------------- Date : August 2012 Copyright : (C) 2012 by Victor Olaya Email : volayaf at gmail dot com *************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * *************************************************************************** """ from builtins import str __author__ = 'Victor Olaya' __date__ = 'August 2012' __copyright__ = '(C) 2012, Victor Olaya' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' from qgis.PyQt.QtCore import QCoreApplication import os import re import json ALG_DESC = 'ALG_DESC' ALG_CREATOR = 'ALG_CREATOR' ALG_HELP_CREATOR = 'ALG_HELP_CREATOR' ALG_VERSION = 'ALG_VERSION' exps = [(r"\*(.*?)\*", r"\1"), ("``(.*?)``", r'\1'), ("(.*?)\n==+\n+?", r'

\1

'), ("(.*?)\n--+\n+?", r'

\1

'), (r"::(\s*\n(\s*\n)*((\s+).*?\n)(((\4).*?\n)|(\s*\n))*)", r"
\1
"), ("\n+", "

")] def getHtmlFromRstFile(rst): if not os.path.exists(rst): return None with open(rst) as f: lines = f.readlines() s = "".join(lines) for exp, replace in exps: p = re.compile(exp) s = p.sub(replace, s) return s def getHtmlFromHelpFile(alg, helpFile): if not os.path.exists(helpFile): return None try: with open(helpFile) as f: descriptions = json.load(f) return getHtmlFromDescriptionsDict(alg, descriptions) except: return None def getHtmlFromDescriptionsDict(alg, descriptions): s = tr('

Algorithm description

\n') s += '

' + getDescription(ALG_DESC, descriptions) + '

\n' s += tr('

Input parameters

\n') for param in alg.parameters: s += '

' + param.description + '

\n' s += '

' + getDescription(param.name, descriptions) + '

\n' s += tr('

Outputs

\n') for out in alg.outputs: s += '

' + out.description + '

\n' s += '

' + getDescription(out.name, descriptions) + '

\n' s += '
' s += tr('

Algorithm author: %s

') % getDescription(ALG_CREATOR, descriptions) s += tr('

Help author: %s

') % getDescription(ALG_HELP_CREATOR, descriptions) s += tr('

Algorithm version: %s

') % getDescription(ALG_VERSION, descriptions) s += '' return s def getDescription(name, descriptions): if name in descriptions: return str(descriptions[name]).replace("\n", "
") else: return '' def tr(string): return QCoreApplication.translate('Help2Html', string)