build_class_graphical.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: build_class_graphical
  6. # AUTHOR(S): Vaclav Petras <wenzeslaus gmail com>
  7. # PURPOSE: Build page with modules per family/class/category with images
  8. # COPYRIGHT: (C) 2015 by Vaclav Petras and the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (>=v2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. #
  14. #############################################################################
  15. import sys
  16. import os
  17. import fnmatch
  18. #from build_html import *
  19. from build_html import (
  20. default_year, header1_tmpl, grass_version,
  21. modclass_intro_tmpl, to_title, html_files,
  22. check_for_desc_override, get_desc, write_html_footer, replace_file,
  23. )
  24. header_graphical_index_tmpl = """\
  25. <link rel="stylesheet" href="grassdocs.css" type="text/css">
  26. <style>
  27. .img-list {
  28. margin: 0;
  29. padding: 0;
  30. list-style-type: none;
  31. }
  32. .img-list li {
  33. padding: 5px;
  34. overflow: auto;
  35. }
  36. .img-list li:hover {
  37. background-color: #eee;
  38. }
  39. .img-list li a {
  40. color: initial;
  41. text-decoration: none;
  42. display: block;
  43. }
  44. .img-list li img {
  45. width: 10%;
  46. float: left;
  47. margin: 0 15px 0 0;
  48. background: white;
  49. object-fit: scale-down;
  50. }
  51. .img-list li img.default-img {
  52. max-height: 5ex;
  53. }
  54. .img-list li .desc {
  55. margin: 0px;
  56. }
  57. .img-list li .name {
  58. margin: 5px;
  59. display: block;
  60. color: #409940;
  61. font-weight: bold;
  62. font-style: italic;
  63. }
  64. </style>
  65. </head>
  66. <body style="width: 99%">
  67. <div id="container">
  68. <a href="index.html"><img src="grass_logo.png" alt="GRASS logo"></a>
  69. <hr class="header">
  70. <h2>Graphical index of GRASS GIS modules</h2>
  71. """
  72. def file_matches(filename, patterns):
  73. for pattern in patterns:
  74. if fnmatch.fnmatch(filename, pattern):
  75. return True
  76. return False
  77. def starts_with_module(string, module):
  78. # not solving:
  79. # module = module.replace('wxGUI.', 'g.gui.')
  80. # TODO: matches g.mapsets images for g.mapset and d.rast.num for d.rast
  81. if string.startswith(module.replace('.', '_')):
  82. return True
  83. if string.startswith(module.replace('.', '')):
  84. return True
  85. if string.startswith(module):
  86. return True
  87. return False
  88. def get_module_image(module, images):
  89. candidates = []
  90. for image in images:
  91. if starts_with_module(image, module):
  92. candidates.append(image)
  93. if len(candidates) == 1:
  94. # matches g.mapsets images for g.mapset and d.rast.num for d.rast
  95. return candidates[0]
  96. if not candidates:
  97. return None
  98. for image in candidates:
  99. basename, unused = image.rsplit('.', 1)
  100. if basename == module.replace('.', '_'):
  101. return image
  102. if basename == module.replace('.', ''):
  103. return image
  104. if basename == module:
  105. return image
  106. return sorted(candidates, key=len)[0]
  107. def generate_page_for_category(short_family, module_family, imgs, year,
  108. skip_no_image=False):
  109. filename = module_family + "_graphical.html"
  110. output = open(filename + ".tmp", 'w')
  111. output.write(header1_tmpl.substitute(
  112. title="GRASS GIS %s Reference "
  113. "Manual: Graphical index" % grass_version))
  114. output.write(header_graphical_index_tmpl)
  115. if module_family.lower() not in ['general', 'postscript']:
  116. if module_family == 'raster3d':
  117. # covert keyword to nice form
  118. module_family = '3D raster'
  119. output.write(modclass_intro_tmpl.substitute(
  120. modclass=module_family, modclass_lower=module_family.lower()))
  121. if module_family == 'wxGUI':
  122. output.write("<h3>wxGUI components:</h3>")
  123. elif module_family == 'guimodules':
  124. output.write("<h3>g.gui.* modules:</h3>")
  125. else:
  126. output.write("<h3>{0} modules:</h3>".format(to_title(module_family)))
  127. output.write('<ul class="img-list">')
  128. #for all modules:
  129. for cmd in html_files(short_family, ignore_gui=False):
  130. basename = os.path.splitext(cmd)[0]
  131. desc = check_for_desc_override(basename)
  132. if desc is None:
  133. desc = get_desc(cmd)
  134. img = get_module_image(basename, imgs)
  135. img_class = 'linkimg'
  136. if skip_no_image and not img:
  137. continue
  138. elif not img:
  139. img = 'grass_logo.png'
  140. img_class = 'default-img'
  141. if basename.startswith('wxGUI'):
  142. basename = basename.replace('.', ' ')
  143. output.write(
  144. '<li>'
  145. '<a href="{html}">'
  146. '<img class="{img_class}" src="{img}">'
  147. '<span class="name">{name}</span> '
  148. '<span class="desc">{desc}</span>'
  149. '</a>'
  150. '</li>'
  151. .format(html=cmd, img=img, name=basename,
  152. desc=desc, img_class=img_class))
  153. output.write('</ul>')
  154. write_html_footer(output, "index.html", year)
  155. output.close()
  156. replace_file(filename)
  157. # TODO: dependencies in makefile for this have to be fixed
  158. # TODO: there is a potential overlap with other scripts (-> refactoring)
  159. def main():
  160. year = default_year
  161. html_dir = sys.argv[1]
  162. os.chdir(html_dir)
  163. img_extensions = ['png', 'jpg', 'gif']
  164. img_patterns = ['*.' + extension for extension in img_extensions]
  165. imgs = []
  166. for filename in sorted(os.listdir(html_dir)):
  167. if file_matches(filename, img_patterns):
  168. imgs.append(filename)
  169. # using term family
  170. # category has its meaning in GRASS already
  171. # class has its meaning in Python, plus it is a synonym for category
  172. # TODO: what would be user friendly is unclear
  173. families = [
  174. ('d', 'display'),
  175. ('db', 'database'),
  176. ('g', 'general'),
  177. ('i', 'imagery'),
  178. ('m', 'miscellaneous'),
  179. ('ps', 'postscript'),
  180. ('r', 'raster'),
  181. ('r3', 'raster3d'),
  182. ('t', 'temporal'),
  183. ('v', 'vector'),
  184. ('wxGUI', 'wxGUI'),
  185. ('g.gui', 'guimodules'),
  186. ]
  187. # partial compatibility with build_class.py
  188. # first arg is dist html dir but the 3 other are like first 3 there
  189. if len(sys.argv) > 2:
  190. short_family = sys.argv[2]
  191. module_family = sys.argv[3]
  192. classes = [(short_family, module_family)]
  193. if len(sys.argv) > 4:
  194. year = sys.argv[4]
  195. for short_family, module_family in families:
  196. generate_page_for_category(short_family, module_family, imgs,
  197. year=year, skip_no_image=False)
  198. if __name__ == '__main__':
  199. main()