""" *************************************************************************** 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. * * * *************************************************************************** """ __author__ = "Victor Olaya" __date__ = "August 2012" __copyright__ = "(C) 2012, Victor Olaya" import os import re import json from qgis.PyQt.QtCore import QCoreApplication, QUrl from processing.tools import system 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) content = getHtmlFromDescriptionsDict(alg, descriptions) algGroup, algName = alg.id().split(":") filePath = os.path.join(system.tempHelpFolder(), f"{algGroup}_{algName}.html") with open(filePath, "w", encoding="utf-8") as f: f.write(content) return QUrl.fromLocalFile(filePath).toString() 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.parameterDefinitions(): 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: {0}

').format( getDescription(ALG_CREATOR, descriptions) ) s += tr('

Help author: {0}

').format( getDescription(ALG_HELP_CREATOR, descriptions) ) s += tr('

Algorithm version: {0}

').format( 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)