Help2Html.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """
  2. ***************************************************************************
  3. Help2Html.py
  4. ---------------------
  5. Date : August 2012
  6. Copyright : (C) 2012 by Victor Olaya
  7. Email : volayaf at gmail dot com
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Victor Olaya'
  18. __date__ = 'August 2012'
  19. __copyright__ = '(C) 2012, Victor Olaya'
  20. import os
  21. import re
  22. import json
  23. from qgis.PyQt.QtCore import QCoreApplication, QUrl
  24. from processing.tools import system
  25. ALG_DESC = 'ALG_DESC'
  26. ALG_CREATOR = 'ALG_CREATOR'
  27. ALG_HELP_CREATOR = 'ALG_HELP_CREATOR'
  28. ALG_VERSION = 'ALG_VERSION'
  29. exps = [(r"\*(.*?)\*", r"<i>\1</i>"),
  30. ("``(.*?)``", r'<FONT FACE="courier">\1</FONT>'),
  31. ("(.*?)\n==+\n+?", r'<h2>\1</h2>'),
  32. ("(.*?)\n--+\n+?", r'<h3>\1</h3>'),
  33. (r"::(\s*\n(\s*\n)*((\s+).*?\n)(((\4).*?\n)|(\s*\n))*)", r"<pre>\1</pre>"),
  34. ("\n+", "</p><p>")]
  35. def getHtmlFromRstFile(rst):
  36. if not os.path.exists(rst):
  37. return None
  38. with open(rst) as f:
  39. lines = f.readlines()
  40. s = "".join(lines)
  41. for exp, replace in exps:
  42. p = re.compile(exp)
  43. s = p.sub(replace, s)
  44. return s
  45. def getHtmlFromHelpFile(alg, helpFile):
  46. if not os.path.exists(helpFile):
  47. return None
  48. try:
  49. with open(helpFile) as f:
  50. descriptions = json.load(f)
  51. content = getHtmlFromDescriptionsDict(alg, descriptions)
  52. algGroup, algName = alg.id().split(':')
  53. filePath = os.path.join(system.tempHelpFolder(), f"{algGroup}_{algName}.html")
  54. with open(filePath, 'w', encoding='utf-8') as f:
  55. f.write(content)
  56. return QUrl.fromLocalFile(filePath).toString()
  57. except:
  58. return None
  59. def getHtmlFromDescriptionsDict(alg, descriptions):
  60. s = tr('<html><body><h2>Algorithm description</h2>\n')
  61. s += '<p>' + getDescription(ALG_DESC, descriptions) + '</p>\n'
  62. s += tr('<h2>Input parameters</h2>\n')
  63. for param in alg.parameterDefinitions():
  64. s += '<h3>' + param.description() + '</h3>\n'
  65. s += '<p>' + getDescription(param.name(), descriptions) + '</p>\n'
  66. s += tr('<h2>Outputs</h2>\n')
  67. for out in alg.outputs:
  68. s += '<h3>' + out.description() + '</h3>\n'
  69. s += '<p>' + getDescription(out.name(), descriptions) + '</p>\n'
  70. s += '<br>'
  71. s += tr('<p align="right">Algorithm author: {0}</p>').format(getDescription(ALG_CREATOR, descriptions))
  72. s += tr('<p align="right">Help author: {0}</p>').format(getDescription(ALG_HELP_CREATOR, descriptions))
  73. s += tr('<p align="right">Algorithm version: {0}</p>').format(getDescription(ALG_VERSION, descriptions))
  74. s += '</body></html>'
  75. return s
  76. def getDescription(name, descriptions):
  77. if name in descriptions:
  78. return str(descriptions[name]).replace("\n", "<br>")
  79. else:
  80. return ''
  81. def tr(string):
  82. return QCoreApplication.translate('Help2Html', string)