From b5e646daa0620ec92f0953730acdd2f4369dbd54 Mon Sep 17 00:00:00 2001
From: Laurent Destailleur <eldy@destailleur.fr>
Date: Tue, 21 Mar 2017 16:29:31 +0100
Subject: [PATCH] NEW Show by default README.md file found into root dir of ext
 module.

---
 htdocs/admin/modulehelp.php                   |  2 +-
 htdocs/core/lib/parsemd.lib.php               | 18 ++++--
 htdocs/core/modules/DolibarrModules.class.php | 58 ++++++++++++++++---
 htdocs/theme/eldy/style.css.php               |  7 +++
 htdocs/theme/md/style.css.php                 |  7 +++
 5 files changed, 79 insertions(+), 13 deletions(-)

diff --git a/htdocs/admin/modulehelp.php b/htdocs/admin/modulehelp.php
index e76627c2965..5b428443248 100644
--- a/htdocs/admin/modulehelp.php
+++ b/htdocs/admin/modulehelp.php
@@ -338,7 +338,7 @@ if ($mode == 'desc')
     $text.='<br>';
 
     $moduledesclong=$objMod->getDescLong();
-    if ($moduledesclong) $text.='<br><hr><br>'.$moduledesclong.'<br>';
+    if ($moduledesclong) $text.='<br><hr><div class="moduledesclong">'.$moduledesclong.'<div>';
 }
 
 if ($mode == 'feature')
diff --git a/htdocs/core/lib/parsemd.lib.php b/htdocs/core/lib/parsemd.lib.php
index 17e64f6543e..1eb962463c0 100644
--- a/htdocs/core/lib/parsemd.lib.php
+++ b/htdocs/core/lib/parsemd.lib.php
@@ -24,12 +24,22 @@
 /**
  * Function to parse MD content into HTML
  *
- * @param	string	  $content			MD content
- * @param   string    $parser           'parsedown' or 'nl2br'
- * @return	string                      Parsed content
+ * @param	string	  $content			    MD content
+ * @param   string    $parser               'parsedown' or 'nl2br'
+ * @param   string    $replaceimagepath     Replace path to image with another path. Exemple: ('doc/'=>'xxx/aaa/')
+ * @return	string                          Parsed content
  */
-function dolMd2Html($content, $parser='parsedown')
+function dolMd2Html($content, $parser='parsedown',$replaceimagepath=null)
 {
+    if (is_array($replaceimagepath))
+    {
+        foreach($replaceimagepath as $key => $val)
+        {
+            $keytoreplace = ']('.$key;
+            $valafter = ']('.$val;
+            $content = preg_replace('/'.preg_quote($keytoreplace,'/').'/m', $valafter, $content);
+        }
+    }
     if ($parser == 'parsedown')
     {
         include DOL_DOCUMENT_ROOT.'/includes/parsedown/Parsedown.php';
diff --git a/htdocs/core/modules/DolibarrModules.class.php b/htdocs/core/modules/DolibarrModules.class.php
index 84538801add..48ce35f1532 100644
--- a/htdocs/core/modules/DolibarrModules.class.php
+++ b/htdocs/core/modules/DolibarrModules.class.php
@@ -582,26 +582,68 @@ class DolibarrModules           // Can not be abstract, because we need to insta
     }
 
     /**
-     * Gives the translated module description if translation exists in admin.lang or the default module description
+     * Gives the long description of a module. First check README-la_LA.md then README.md
+     * If not markdown files found, it return translated value of the key ->descriptionlong.
      *
-     * @return  string  Translated module description
+     * @return  string  Long description of a module
      */
     function getDescLong()
     {
         global $langs;
         $langs->load("admin");
         
-        if (empty($this->descriptionlong)) return '';
+        include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
+        include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
+
+        $filefound= false;
         
-        // If module description translation does not exist using its unique id, we can use its name to find translation
-        if (is_array($this->langfiles))
+        // Define path to file README.md. 
+        // First check README-la_LA.md then README.md
+        $pathoffile = dol_buildpath(strtolower($this->name).'/README-'.$langs->defaultlang.'.md', 0);
+        if (dol_is_file($pathoffile))
         {
-            foreach($this->langfiles as $val)
+            $filefound = true;
+        }
+        if (! $filefound)
+        {
+            $pathoffile = dol_buildpath(strtolower($this->name).'/README.md', 0);
+            if (dol_is_file($pathoffile))
             {
-                if ($val) $langs->load($val);
+                $filefound = true;
             }
         }
-        return $langs->trans($this->descriptionlong);
+        
+        if ($filefound)     // Mostly for external modules
+        {
+            $content = file_get_contents($pathoffile);
+    
+            if ((float) DOL_VERSION >= 6.0)
+            {
+                @include_once DOL_DOCUMENT_ROOT.'/core/lib/parsemd.lib.php';
+                $content = dolMd2Html($content, 'parsedown', array('doc/'=>dol_buildpath('cabinetmed/doc/', 1)));
+            }
+            else
+            {
+                $content = nl2br($content);
+            }
+        }
+        else                // Mostly for internal modules
+        {
+            if (! empty($this->descriptionlong))
+            {
+                if (is_array($this->langfiles))
+                {
+                    foreach($this->langfiles as $val)
+                    {
+                        if ($val) $langs->load($val);
+                    }
+                }
+            
+                $content = $langs->trans($this->descriptionlong);
+            }
+        }
+    
+        return $content;
     }
     
     /**
diff --git a/htdocs/theme/eldy/style.css.php b/htdocs/theme/eldy/style.css.php
index 3d6e8868bec..26a0c8d21ed 100644
--- a/htdocs/theme/eldy/style.css.php
+++ b/htdocs/theme/eldy/style.css.php
@@ -632,6 +632,13 @@ div.myavailability {
 	margin-bottom: 12px;
 }
 
+/* For the long description of module */
+.moduledesclong p img, .moduledesclong p a img {
+    max-width: 90% !important;
+    height: auto !important;
+}
+
+
 /* DOL_XXX for future usage (when left menu has been removed). If we do not use datatable */
 /*.table-responsive {
     width: calc(100% - 330px);
diff --git a/htdocs/theme/md/style.css.php b/htdocs/theme/md/style.css.php
index aae0fd7f677..c930b82dd8c 100644
--- a/htdocs/theme/md/style.css.php
+++ b/htdocs/theme/md/style.css.php
@@ -640,6 +640,13 @@ div.myavailability {
 	margin-bottom: 12px;
 }
 
+/* For the long description of module */
+.moduledesclong p img,.moduledesclong p a img {
+    max-width: 90% !important;
+    height: auto !important;
+}
+
+
 /* DOL_XXX for future usage (when left menu has been removed). If we do not use datatable */
 /*.table-responsive {
     width: calc(100% - 330px);
-- 
GitLab