build_class_graphical.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #!/usr/bin/env python
  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,
  107. lambda first, second: cmp(len(first), len(second)))[0]
  108. def generate_page_for_category(short_family, module_family, imgs, year,
  109. skip_no_image=False):
  110. filename = module_family + "_graphical.html"
  111. output = open(filename + ".tmp", 'wb')
  112. output.write(header1_tmpl.substitute(
  113. title="GRASS GIS %s Reference "
  114. "Manual: Graphical index" % grass_version))
  115. output.write(header_graphical_index_tmpl)
  116. if module_family.lower() not in ['general', 'postscript']:
  117. if module_family == 'raster3d':
  118. # covert keyword to nice form
  119. module_family = '3D raster'
  120. output.write(modclass_intro_tmpl.substitute(
  121. modclass=module_family, modclass_lower=module_family.lower()))
  122. if module_family == 'wxGUI':
  123. output.write("<h3>wxGUI components:</h3>")
  124. elif module_family == 'guimodules':
  125. output.write("<h3>g.gui.* modules:</h3>")
  126. else:
  127. output.write("<h3>{0} modules:</h3>".format(to_title(module_family)))
  128. output.write('<ul class="img-list">')
  129. #for all modules:
  130. for cmd in html_files(short_family, ignore_gui=False):
  131. basename = os.path.splitext(cmd)[0]
  132. desc = check_for_desc_override(basename)
  133. if desc is None:
  134. desc = get_desc(cmd)
  135. img = get_module_image(basename, imgs)
  136. img_class = 'linkimg'
  137. if skip_no_image and not img:
  138. continue
  139. elif not img:
  140. img = 'grass_logo.png'
  141. img_class = 'default-img'
  142. if basename.startswith('wxGUI'):
  143. basename = basename.replace('.', ' ')
  144. output.write(
  145. '<li>'
  146. '<a href="{html}">'
  147. '<img class="{img_class}" src="{img}">'
  148. '<span class="name">{name}</span> '
  149. '<span class="desc">{desc}</span>'
  150. '</a>'
  151. '</li>'
  152. .format(html=cmd, img=img, name=basename,
  153. desc=desc, img_class=img_class))
  154. output.write('</ul>')
  155. write_html_footer(output, "index.html", year)
  156. output.close()
  157. replace_file(filename)
  158. # TODO: dependencies in makefile for this have to be fixed
  159. # TODO: there is a potential overlap with other scripts (-> refactoring)
  160. def main():
  161. year = default_year
  162. html_dir = sys.argv[1]
  163. os.chdir(html_dir)
  164. img_extensions = ['png', 'jpg', 'gif']
  165. img_patterns = ['*.' + extension for extension in img_extensions]
  166. imgs = []
  167. for filename in os.listdir(html_dir):
  168. if file_matches(filename, img_patterns):
  169. imgs.append(filename)
  170. # using term family
  171. # category has its meaning in GRASS already
  172. # class has its meaning in Python, plus it is a synonym for category
  173. # TODO: what would be user friendly is unclear
  174. families = [
  175. ('d', 'display'),
  176. ('db', 'database'),
  177. ('g', 'general'),
  178. ('i', 'imagery'),
  179. ('m', 'miscellaneous'),
  180. ('ps', 'postscript'),
  181. ('r', 'raster'),
  182. ('r3', 'raster3d'),
  183. ('t', 'temporal'),
  184. ('v', 'vector'),
  185. ('wxGUI', 'wxGUI'),
  186. ('g.gui', 'guimodules'),
  187. ]
  188. # partial compatibility with build_class.py
  189. # first arg is dist html dir but the 3 other are like first 3 there
  190. if len(sys.argv) > 2:
  191. short_family = sys.argv[2]
  192. module_family = sys.argv[3]
  193. classes = [(short_family, module_family)]
  194. if len(sys.argv) > 4:
  195. year = sys.argv[4]
  196. for short_family, module_family in families:
  197. generate_page_for_category(short_family, module_family, imgs,
  198. year=year, skip_no_image=True)
  199. if __name__ == '__main__':
  200. main()