Changeset 66517


Ignore:
Timestamp:
Oct 16, 2015, 8:44:52 AM (9 years ago)
Author:
wenzeslaus
Message:

Python port of description extraction for addons

The new version drops adding li end tag which was not working.
It also trips leading space and adds this to the caller script which
now adds space after colon.

Adding test files and script to quickly test the standalone script.

Location:
grass-addons/tools/addons
Files:
7 added
1 edited
1 moved

Legend:

Unmodified
Added
Removed
  • grass-addons/tools/addons/get_page_description.py

    r66516 r66517  
    1 #!/bin/sh
     1#!/usr/bin/env python
    22
    33# PURPOSE: Extracts page one line descriptions for index.html of GRASS GIS Addons
    44
    5 # AUTHORS: Martin Landa
     5# AUTHORS: Martin Landa (Bash version)
     6#          Vaclav Petras (Python version)
    67
    7 if [ $# -ne 1 ]; then
    8     echo "$(basename $0) takes exactly one argument (HTML manual page name)"
    9     exit 1
    10 fi
     8import os
     9import sys
     10import re
    1111
    12 TMP=$$
    1312
    14 currfile=$1
     13def get_desc_from_manual_page_line(text):
     14    """
     15    >>> get_desc_from_manual_page_line("r.example - This is a description<br>")
     16    'This is a description'
     17    """
     18    # this matches the dash at the beginning
     19    text = text.split(" - ", 1)[1]
     20    # this matches a tag at the end
     21    # (supposing no tags in the description and < represented as &lt;
     22    text = text.split("<", 1)[0]
     23    return text
    1524
    16 grep 'KEYWORDS' $currfile 2> /dev/null > /dev/null
    17 if [ $? -eq 0 ] ; then
    18     # keywords found, so go ahead with extraction of one-line description
    19     cat $currfile | awk '/NAME/,/KEYWORDS/' | grep ' - ' | cut -d'-' -f2- | cut -d'<' -f1 | sed 's+>$+></li>+g'  >> /tmp/d.$TMP
    20     # argh, fake keyword line found (broken manual page or missing g.parser usage)
    21     if [ ! -s /tmp/d.$TMP ] ; then
    22         echo "(incomplete manual page, please fix; name part not found)" > /tmp/d.$TMP
    23     fi
    24     cat /tmp/d.$TMP
    25     rm -f /tmp/d.$TMP
    26 else
    27     # let's try to be more robust against missing keywords in a few HTML pages
    28     # argh, no keywords found (broken manual page or missing g.parser usage)
    29     echo "(incomplete manual page, please fix; keyword part not found)"
    30 fi
     25
     26def main(filename):
     27    with open(filename) as page_file:
     28        desc = None
     29        in_desc_block = False
     30        desc_block_start = re.compile(r'NAME')
     31        desc_block_end = re.compile(r'KEYWORDS')
     32        desc_line = re.compile(r' - ')
     33        for line in page_file:
     34            line = line.rstrip()  # remove '\n' at end of line
     35            if desc_block_start.search(line):
     36                in_desc_block = True
     37            elif desc_block_end.search(line):
     38                in_desc_block = False
     39            if in_desc_block:
     40                if desc_line.search(line):
     41                    desc = get_desc_from_manual_page_line(line)
     42        if not desc:
     43            desc = "(incomplete manual page, please fix)"
     44        # the original script attempted to add also </li> but it as not working
     45        # now we leave up to the caller as well as whitespace around
     46        print desc
     47
     48
     49if __name__ == "__main__":
     50    if len(sys.argv) != 2:
     51        sys.exit("{name} takes exactly one argument (HTML manual page name)."
     52                 " {argc} parameters were given."
     53                 .format(name=os.path.basename(sys.argv[0]),
     54                                               argc=len(sys.argv) - 1))
     55    sys.exit(main(sys.argv[1]))
  • grass-addons/tools/addons/grass-addons-index.sh

    r66516 r66517  
    123123
    124124        module=`echo $currfile | sed 's+\.html$++g'`
    125         echo "<li style=\"margin-left: 20px\"><a href=\"$currfile\">$module</a>:" >> index.html
    126         ${SRC}grass-addons/tools/addons/get_page_description.sh $currfile >> index.html
     125        echo "<li style=\"margin-left: 20px\"><a href=\"$currfile\">$module</a>: " >> index.html
     126        ${SRC}grass-addons/tools/addons/get_page_description.py $currfile >> index.html
    127127    done
    128128
Note: See TracChangeset for help on using the changeset viewer.