2012-10-05 23:28:47 +02:00
|
|
|
# -*- 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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2014-05-31 18:53:43 +02:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-05 23:28:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
import os
|
2014-05-31 18:53:43 +02:00
|
|
|
import re
|
|
|
|
import json
|
2013-05-24 00:38:29 +02:00
|
|
|
|
2017-06-06 10:13:18 +03:00
|
|
|
from qgis.PyQt.QtCore import QCoreApplication, QUrl
|
|
|
|
|
|
|
|
from processing.tools import system
|
|
|
|
|
2014-04-19 00:48:56 +02:00
|
|
|
ALG_DESC = 'ALG_DESC'
|
|
|
|
ALG_CREATOR = 'ALG_CREATOR'
|
|
|
|
ALG_HELP_CREATOR = 'ALG_HELP_CREATOR'
|
2014-06-02 22:43:47 +02:00
|
|
|
ALG_VERSION = 'ALG_VERSION'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2014-04-19 00:48:56 +02:00
|
|
|
exps = [(r"\*(.*?)\*", r"<i>\1</i>"),
|
2014-04-19 22:04:24 +02:00
|
|
|
("``(.*?)``", r'<FONT FACE="courier">\1</FONT>'),
|
2014-04-19 00:48:56 +02:00
|
|
|
("(.*?)\n==+\n+?", r'<h2>\1</h2>'),
|
|
|
|
("(.*?)\n--+\n+?", r'<h3>\1</h3>'),
|
2015-08-22 14:29:41 +02:00
|
|
|
(r"::(\s*\n(\s*\n)*((\s+).*?\n)(((\4).*?\n)|(\s*\n))*)", r"<pre>\1</pre>"),
|
2014-04-19 00:48:56 +02:00
|
|
|
("\n+", "</p><p>")]
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2014-04-19 00:48:56 +02:00
|
|
|
def getHtmlFromRstFile(rst):
|
2014-04-24 17:25:36 +02:00
|
|
|
if not os.path.exists(rst):
|
2014-06-08 19:57:22 +02:00
|
|
|
return None
|
2014-04-19 00:48:56 +02:00
|
|
|
with open(rst) as f:
|
2014-04-19 22:04:24 +02:00
|
|
|
lines = f.readlines()
|
2014-04-19 00:48:56 +02:00
|
|
|
s = "".join(lines)
|
|
|
|
for exp, replace in exps:
|
|
|
|
p = re.compile(exp)
|
|
|
|
s = p.sub(replace, s)
|
|
|
|
return s
|
2014-04-19 22:04:24 +02:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2014-04-19 00:48:56 +02:00
|
|
|
def getHtmlFromHelpFile(alg, helpFile):
|
|
|
|
if not os.path.exists(helpFile):
|
|
|
|
return None
|
2014-06-10 18:35:28 +02:00
|
|
|
try:
|
2014-06-13 09:03:15 +02:00
|
|
|
with open(helpFile) as f:
|
2014-06-10 18:35:28 +02:00
|
|
|
descriptions = json.load(f)
|
2017-06-06 10:13:18 +03:00
|
|
|
|
|
|
|
content = getHtmlFromDescriptionsDict(alg, descriptions)
|
|
|
|
algGroup, algName = alg.id().split(':')
|
|
|
|
filePath = os.path.join(system.tempHelpFolder(), "{}_{}.html".format(algGroup, algName))
|
|
|
|
with open(filePath, 'w', encoding='utf-8') as f:
|
|
|
|
f.write(content)
|
|
|
|
return QUrl.fromLocalFile(filePath).toString()
|
2014-06-10 18:35:28 +02:00
|
|
|
except:
|
|
|
|
return None
|
2014-09-23 10:13:21 +02:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2014-09-23 10:13:21 +02:00
|
|
|
def getHtmlFromDescriptionsDict(alg, descriptions):
|
2014-10-03 21:56:24 +03:00
|
|
|
s = tr('<html><body><h2>Algorithm description</h2>\n')
|
2014-04-19 00:48:56 +02:00
|
|
|
s += '<p>' + getDescription(ALG_DESC, descriptions) + '</p>\n'
|
2014-10-03 21:56:24 +03:00
|
|
|
s += tr('<h2>Input parameters</h2>\n')
|
2017-05-16 16:36:00 +10:00
|
|
|
for param in alg.parameterDefinitions():
|
|
|
|
s += '<h3>' + param.description() + '</h3>\n'
|
|
|
|
s += '<p>' + getDescription(param.name(), descriptions) + '</p>\n'
|
2014-10-03 21:56:24 +03:00
|
|
|
s += tr('<h2>Outputs</h2>\n')
|
2014-04-19 00:48:56 +02:00
|
|
|
for out in alg.outputs:
|
2017-05-16 16:36:00 +10:00
|
|
|
s += '<h3>' + out.description() + '</h3>\n'
|
|
|
|
s += '<p>' + getDescription(out.name(), descriptions) + '</p>\n'
|
2014-04-19 00:48:56 +02:00
|
|
|
s += '<br>'
|
2017-03-04 16:23:36 +01:00
|
|
|
s += tr('<p align="right">Algorithm author: {0}</p>').format(getDescription(ALG_CREATOR, descriptions))
|
|
|
|
s += tr('<p align="right">Help author: {0}</p>').format(getDescription(ALG_HELP_CREATOR, descriptions))
|
|
|
|
s += tr('<p align="right">Algorithm version: {0}</p>').format(getDescription(ALG_VERSION, descriptions))
|
2014-04-19 00:48:56 +02:00
|
|
|
s += '</body></html>'
|
|
|
|
return s
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2014-04-19 00:48:56 +02:00
|
|
|
def getDescription(name, descriptions):
|
|
|
|
if name in descriptions:
|
2016-09-21 18:24:26 +02:00
|
|
|
return str(descriptions[name]).replace("\n", "<br>")
|
2014-04-19 00:48:56 +02:00
|
|
|
else:
|
|
|
|
return ''
|
2014-06-02 22:43:47 +02:00
|
|
|
|
2015-08-22 14:29:41 +02:00
|
|
|
|
2014-10-03 21:56:24 +03:00
|
|
|
def tr(string):
|
2014-10-08 18:57:25 +02:00
|
|
|
return QCoreApplication.translate('Help2Html', string)
|