From ad2e69b51e38506d176df1d41d3302a3d3eecefc Mon Sep 17 00:00:00 2001
From: Victor Poughon <victor.poughon@cnes.fr>
Date: Mon, 10 Dec 2018 18:34:38 +0100
Subject: [PATCH] DOC: add automatic internal links in app description

---
 .../Scripts/otbGenerateWrappersRstDoc.py      | 20 ++++++++++++++++---
 .../Cookbook/rst/templates/application.rst    |  2 ++
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/Documentation/Cookbook/Scripts/otbGenerateWrappersRstDoc.py b/Documentation/Cookbook/Scripts/otbGenerateWrappersRstDoc.py
index 902918588b..c06acc8035 100755
--- a/Documentation/Cookbook/Scripts/otbGenerateWrappersRstDoc.py
+++ b/Documentation/Cookbook/Scripts/otbGenerateWrappersRstDoc.py
@@ -22,6 +22,7 @@
 import os
 import sys
 import argparse
+import re
 
 import otbApplication
 from otbApplication import ParameterType_Bool, ParameterType_Int, ParameterType_Radius, ParameterType_RAM, ParameterType_Float, ParameterType_String, ParameterType_StringList, ParameterType_InputFilename, ParameterType_OutputFilename, ParameterType_InputImage, ParameterType_ComplexInputImage, ParameterType_OutputImage, ParameterType_ComplexOutputImage, ParameterType_InputVectorData, ParameterType_OutputVectorData, ParameterType_Directory, ParameterType_Choice, ParameterType_InputImageList, ParameterType_InputVectorDataList, ParameterType_InputFilenameList, ParameterType_InputProcessXML, ParameterType_OutputProcessXML, ParameterType_ListView, ParameterType_Group
@@ -355,7 +356,19 @@ def render_see_also(app):
     else:
         return rst_section("See also", "-") + see_also
 
-def render_application(appname):
+def multireplace(string, replacements):
+    "multiple string replace (from https://stackoverflow.com/a/6117124/5815110)"
+    substrs = sorted(replacements, key=len, reverse=True)
+    regexp = re.compile('|'.join(map(re.escape, substrs)))
+    return regexp.sub(lambda match: replacements[match.group(0)], string)
+
+def make_links(text, allapps):
+    "Replace name of applications by internal rst links"
+
+    rep = {appname: ":ref:`{}`".format("app-" + appname) for appname in allapps}
+    return multireplace(text, rep)
+
+def render_application(appname, allapps):
     "Render app to rst"
 
     app = otbApplication.Registry.CreateApplication(appname)
@@ -368,9 +381,10 @@ def render_application(appname):
     parameters = rst_parameters(app)
 
     output = template_application.format(
+        label="app-" + appname,
         heading=rst_section(app.GetName(), '='),
         description=app.GetDescription(),
-        longdescription=app.GetDocLongDescription(),
+        longdescription=make_links(app.GetDocLongDescription(), allapps),
         parameters=parameters,
         examples_cli=render_all_examples_cli(app),
         examples_python=render_all_examples_python(app),
@@ -434,7 +448,7 @@ def GenerateRstForApplications(rst_dir):
         # Write application rst
         #print("Generating " + appName + ".rst" +  " on tag " + tag)
         with open(rst_dir + '/Applications/app_'  + appName + '.rst', 'w') as appFile:
-            appFile.write(render_application(appName))
+            appFile.write(render_application(appName, appNames))
 
 if __name__ == "__main__":
     parser = argparse.ArgumentParser(usage="Export application(s) to rst file")
diff --git a/Documentation/Cookbook/rst/templates/application.rst b/Documentation/Cookbook/rst/templates/application.rst
index a05c9699e3..68db4a8b3c 100644
--- a/Documentation/Cookbook/rst/templates/application.rst
+++ b/Documentation/Cookbook/rst/templates/application.rst
@@ -1,3 +1,5 @@
+.. _{label}:
+
 {heading}
 {description}
 
-- 
GitLab