[GRASS-SVN] r72800 - sandbox/wenzeslaus/g.citation

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Jun 11 18:19:03 PDT 2018


Author: wenzeslaus
Date: 2018-06-11 18:19:03 -0700 (Mon, 11 Jun 2018)
New Revision: 72800

Modified:
   sandbox/wenzeslaus/g.citation/g.citation.py
Log:
g.citation: get code and hist URLs from the doc

Modified: sandbox/wenzeslaus/g.citation/g.citation.py
===================================================================
--- sandbox/wenzeslaus/g.citation/g.citation.py	2018-06-12 00:54:29 UTC (rev 72799)
+++ sandbox/wenzeslaus/g.citation/g.citation.py	2018-06-12 01:19:03 UTC (rev 72800)
@@ -213,6 +213,26 @@
     return authors
 
 
+def get_code_urls_from_documentation(text):
+    """Extract URLs from text containing links to module source code
+
+    Returns a tuple with URL of the source code and URL of history of
+    the source code.
+
+    >>> text = '<h2>SOURCE CODE</h2><a href="http://osgeo.org/r.spread">r.spread source code</a> (<a href="http://osgeo.org/log/r.spread">history</a>)'
+    >>> get_code_urls_from_documentation(text)
+    ('http://osgeo.org/r.spread', 'http://osgeo.org/log/r.spread')
+    """
+    capture = r'<h2>SOURCE CODE</h2>.*<a href="(.+)">[^<]*source code</a>\s+\(<a href="(.+)">history</a>\)'
+    match = re.search(capture, text,
+                      re.MULTILINE | re.DOTALL | re.IGNORECASE)
+    if match:
+        return match.group(1), match.group(2)
+    else:
+        # TODO: raise or fatal? should be in library or module?
+        raise RuntimeError("The text does not contain source code URLs")
+
+
 def print_bibtex(citation):
     """Create BibTeX entry from citation dictionary
 
@@ -288,6 +308,9 @@
     citation['module'] = name
     citation['authors'] = get_authors_from_documentation(text)
     citation['year'] = get_year_from_documentation(text)
+    code_url, code_history_url = get_code_urls_from_documentation(text)
+    citation['code-url'] = code_url
+    citation['url-code-history'] = code_history_url
 
     return citation
 



More information about the grass-commit mailing list