build_rest.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. #!/usr/bin/env python3
  2. """
  3. Created on Thu Aug 9 14:04:12 2012
  4. @author: Luca Delucchi
  5. @author: Markus Neteler
  6. @author: Glynn Clements
  7. """
  8. # utilities for generating REST indices
  9. # utilities for generating HTML indices
  10. # (C) 2003-2022 by Luca Delucchi and the GRASS Development Team
  11. import os
  12. import string
  13. # TODO: better fix this in include/Make/Rest.make, see bug RT #5361
  14. # exclude following list of modules from help index:
  15. exclude_mods = [
  16. "i.find",
  17. "r.watershed.ram",
  18. "r.watershed.seg",
  19. "v.topo.check",
  20. "helptext.html",
  21. ]
  22. # these modules don't use G_parser()
  23. desc_override = {
  24. "g.parser": "Provides automated parser, GUI, and help support for GRASS scipts.",
  25. "r.li.daemon": "Support module for r.li landscape index calculations.",
  26. }
  27. ############################################################################
  28. header2_tmpl = string.Template(
  29. r"""
  30. ==================================================================
  31. GRASS GIS ${grass_version} Reference Manual
  32. ==================================================================
  33. .. figure:: grass_logo.png
  34. :align: center
  35. :alt: GRASS logo
  36. GRASS GIS ${grass_version} Reference Manual
  37. --------------------------------------------------------------------
  38. **Geographic Resources Analysis Support System**, commonly
  39. referred to as `GRASS GIS <https://grass.osgeo.org>`_, is a `Geographic
  40. Information System <https://en.wikipedia.org/wiki/Geographic_information_system>`_
  41. (GIS) used for geospatial data management and analysis, image processing,
  42. graphics/maps production, spatial modeling, and visualization. GRASS is
  43. currently used in academic and commercial settings around the world, as
  44. well as by many governmental agencies and environmental consulting companies.
  45. This reference manual details the use of modules distributed with
  46. Geographic Resources Analysis Support System (GRASS), an open source
  47. (`GNU GPLed <https://www.gnu.org/licenses/gpl.html>`_), image
  48. processing and geographic information system (GIS).
  49. """
  50. )
  51. overview_tmpl = string.Template(
  52. r"""
  53. Quick Introduction
  54. ~~~~~~~~~~~~~~~~~~~~
  55. .. toctree::
  56. :maxdepth: 1
  57. How to start with GRASS <helptext.html>
  58. Intro projections and spatial transformations <projectionintro>
  59. Intro 2D raster map processing <rasterintro>
  60. Intro 3D raster map (voxel) processing <raster3dintro>
  61. Intro image processing <imageryintro>
  62. Intro vector map processing and network analysis <vectorintro>
  63. Intro database management <databaseintro>
  64. Intro temporal data processing <temporalintro>
  65. Display/Graphical User Interfaces
  66. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  67. .. toctree::
  68. :maxdepth: 1
  69. wxGUI wxPython-based GUI frontend <wxGUI>
  70. Display commands manual <display>
  71. Display drivers <displaydrivers>
  72. nviz 3D visualization and animation tool <wxGUI.Nviz>
  73. Raster and 3D raster processing
  74. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  75. .. toctree::
  76. :maxdepth: 1
  77. Raster commands manual <raster>
  78. 3D raster (voxel) commands manual <raster3D>
  79. Image processing
  80. ~~~~~~~~~~~~~~~~~~
  81. .. toctree::
  82. :maxdepth: 1
  83. Imagery commands manual <imagery>
  84. Vector processing
  85. ~~~~~~~~~~~~~~~~~~
  86. .. toctree::
  87. :maxdepth: 1
  88. Vector commands manual <vector>
  89. GRASS ASCII vector format specification <vectorascii>
  90. Database
  91. ~~~~~~~~~
  92. .. toctree::
  93. :maxdepth: 1
  94. SQL support in GRASS GIS <sql>
  95. Database commands manual <database>
  96. General
  97. ~~~~~~~~~
  98. .. toctree::
  99. :maxdepth: 1
  100. GRASS startup manual page <grass8>
  101. General commands manual <general>
  102. Miscellaneous & Variables
  103. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  104. .. toctree::
  105. :maxdepth: 1
  106. Miscellaneous commands manual <miscellaneous>
  107. GRASS variables and environment variables <variables>
  108. Temporal processing
  109. ~~~~~~~~~~~~~~~~~~~~
  110. .. toctree::
  111. :maxdepth: 1
  112. Temporal commands manual <temporal>
  113. Printing
  114. ~~~~~~~~~~~~
  115. .. toctree::
  116. :maxdepth: 1
  117. PostScript commands manual <postscript>
  118. """
  119. )
  120. # TODO add copyright symbol
  121. footer_tmpl = string.Template(
  122. r"""
  123. --------------
  124. :doc:`Manual main page <index>` \| :doc:`Full Index <full_index>`
  125. 2003-2022 `GRASS Development Team <https://grass.osgeo.org>`_, GRASS GIS ${grass_version} Reference Manual
  126. """
  127. )
  128. cmd1_tmpl = string.Template(r"""*`$cmd.\* <${cmd}>` *""")
  129. cmd2_tmpl = string.Template(
  130. r"""
  131. ${cmd}.* commands:
  132. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  133. .. toctree::
  134. :maxdepth: 1
  135. """
  136. )
  137. desc1_tmpl = string.Template(
  138. r""" ${basename} - ${desc} <${basename}>
  139. """
  140. )
  141. sections = r"""
  142. +-----------------------------+-------------------------------+
  143. |`d.* <full_index.html#d>`_ | `display commands` |
  144. +-----------------------------+-------------------------------+
  145. |`db.* <full_index.html#db>`_ | `database commands` |
  146. +-----------------------------+-------------------------------+
  147. |`g.* <full_index.html#g>`_ | `general commands` |
  148. +-----------------------------+-------------------------------+
  149. |`i.* <full_index.html#i>`_ | `imagery commands` |
  150. +-----------------------------+-------------------------------+
  151. |`m.* <full_index.html#m>`_ | `miscellaneous commands` |
  152. +-----------------------------+-------------------------------+
  153. |`ps.* <full_index.html#ps>`_ | `postscript commands` |
  154. +-----------------------------+-------------------------------+
  155. |`r.* <full_index.html#r>`_ | `raster commands` |
  156. +-----------------------------+-------------------------------+
  157. |`r3.* <full_index.html#r3>`_ | `raster3D commands` |
  158. +-----------------------------+-------------------------------+
  159. |`t.* <full_index.html#t>`_ | `temporal commands` |
  160. +-----------------------------+-------------------------------+
  161. |`v.* <full_index.html#v>`_ | `vector commands` |
  162. +-----------------------------+-------------------------------+
  163. |`nviz <wxGUI.Nviz.html>`_ | `visualization suite` |
  164. +-----------------------------+-------------------------------+
  165. |`wxGUI <wxGUI.html>`_ | `wxPython-based GUI frontend` |
  166. +-----------------------------+-------------------------------+
  167. """
  168. modclass_intro_tmpl = string.Template(
  169. r"""Go to :doc:`${modclass} introduction <${modclass_lower}intro>`
  170. """
  171. )
  172. # "
  173. modclass_tmpl = string.Template(
  174. r"""Go :doc:`back to help overview<index>`
  175. **${modclass} commands:**
  176. .. toctree::
  177. :maxdepth: 1
  178. """
  179. )
  180. # "
  181. desc2_tmpl = string.Template(
  182. r""" ${basename} - ${desc} <${basename}>
  183. """
  184. )
  185. # "
  186. full_index_header = r"""Go :doc:`back to help overview<index>`
  187. Full command index:
  188. ~~~~~~~~~~~~~~~~~~~~
  189. """
  190. # "
  191. message_tmpl = string.Template(
  192. r"""Generated HTML docs in ${rest_dir}/index.txt
  193. ----------------------------------------------------------------------
  194. Following modules are missing the 'modulename.txt' file in src code:
  195. """
  196. )
  197. def check_for_desc_override(basename):
  198. return desc_override.get(basename)
  199. def read_file(name):
  200. f = open(name, "r")
  201. s = f.read()
  202. f.close()
  203. return s
  204. def write_file(name, contents):
  205. f = open(name, "w")
  206. f.write(contents)
  207. f.close()
  208. def try_mkdir(path):
  209. try:
  210. os.mkdir(path)
  211. except OSError:
  212. pass
  213. def replace_file(name):
  214. temp = name + ".tmp"
  215. if (
  216. os.path.exists(name)
  217. and os.path.exists(temp)
  218. and read_file(name) == read_file(temp)
  219. ):
  220. os.remove(temp)
  221. else:
  222. try:
  223. os.remove(name)
  224. except OSError:
  225. pass
  226. os.rename(temp, name)
  227. def copy_file(src, dst):
  228. write_file(dst, read_file(src))
  229. def rest_files(cls=None):
  230. for cmd in sorted(os.listdir(rest_dir)):
  231. if (
  232. cmd.endswith(".txt")
  233. and (cls in [None, "*"] or cmd.startswith(cls + "."))
  234. and (cls != "*" or len(cmd.split(".")) >= 3)
  235. and cmd not in ["full_index.txt", "index.txt"]
  236. and cmd not in exclude_mods
  237. and not cmd.startswith("wxGUI.")
  238. ):
  239. yield cmd
  240. def write_rest_header(f, title, ismain=False):
  241. f.write(header2_tmpl.substitute(grass_version=grass_version))
  242. def write_rest_cmd_overview(f):
  243. box_color = "#e1ecd0"
  244. f.write(overview_tmpl.substitute(box_color=box_color))
  245. def write_rest_footer(f, index_url):
  246. f.write(footer_tmpl.substitute(grass_version=grass_version, index_url=index_url))
  247. def get_desc(cmd):
  248. f = open(cmd, "r")
  249. while True:
  250. line = f.readline()
  251. if not line:
  252. return ""
  253. if "NAME" in line:
  254. break
  255. while True:
  256. line = f.readline()
  257. if not line:
  258. return ""
  259. if "SYNOPSIS" in line:
  260. break
  261. if "*" in line:
  262. sp = line.split("-", 1)
  263. if len(sp) > 1:
  264. return sp[1].strip()
  265. else:
  266. return None
  267. return ""
  268. # Define global variables
  269. arch_dist_dir = os.environ["ARCH_DISTDIR"]
  270. rest_dir = os.path.join(arch_dist_dir, "docs", "rest")
  271. gisbase = os.environ["GISBASE"]
  272. ver = read_file(os.path.join(gisbase, "etc", "VERSIONNUMBER"))
  273. try:
  274. grass_version = ver.split()[0].strip()
  275. except IndexError:
  276. grass_version = ver.split().strip()