diff --git a/docs/configuration-en.gmi b/docs/configuration-en.gmi index eeb36da..4bac2e9 100644 --- a/docs/configuration-en.gmi +++ b/docs/configuration-en.gmi @@ -1,4 +1,5 @@ # Advanced configuration +❗️ This page works with URL rewriting enabled ❗️ By default, the same style is used for all the pages, //htmgem/css/htmgem.css//, except when a file having the same name as the page but with the extension **.css** exists in the same directory. In such as case, it's used. @@ -16,15 +17,13 @@ Modifying the web server configuration works but it's rather heavy. It's possibl > https://site.tld/htmgem/index.php?url=/url/page.gmi&style=<…> It's also possible to make a URL rewriting which incorporate the style: -> rewrite ^(.+\.gmi)$ /htmgem/?url=$1; -> rewrite ^(.+\.gmi)\ *\|\ *([\w_]+)$ /htmgem/?url=$1&style=$2; +> rewrite ^(.+\.gmi)$ /htmgem/index.php?rw=1&url=$1; +> rewrite ^(.+\.gmi)\ *\|\ *([\w_]+)$ /htmgem/index.php?rw=1&url=$1&style=$2; For instance, when it's activated: => configuration-en.gmi%20|%20pre Shows the readable source code on the screen, using the markup **
**. => configuration-en.gmi%20|%20source configuration-en.gmi | source : Downloads the source code. -=> ../css Sélecteur de style : the page containing the style allows to test them 😎 - ### With style already included… => configuration-en.gmi%20|%20htmgem Style //HtmGem//, the default style @@ -35,6 +34,8 @@ For instance, when it's activated: => configuration-en.gmi%20|%20raw Style //raw// => configuration-en.gmi%20|%20debug Style //debug// +=> ../css Style selector: the page containing the style allows to test them 😎 + ## Text decoration The text decoration, which interprets the **bold** for instance, is not part of GemText definition. The text decoration applies everywhere except on the titles and preformated texts. diff --git a/docs/configuration-fr.gmi b/docs/configuration-fr.gmi index bb56bc9..52f4f91 100644 --- a/docs/configuration-fr.gmi +++ b/docs/configuration-fr.gmi @@ -1,4 +1,5 @@ # Configuration avancée +❗️ Cette page fonctionne avec la réécriture d’URL activée ❗️ Par défaut, le même style est utilisé pour toutes les pages, //htmgem/css/htmgem.css//, sauf lorsqu’un fichier ayant le même nom que la page mais avec l’extension **.css** existe dans le même répertoire. Dans ce cas, il est utilisé. @@ -16,15 +17,13 @@ Modifier la configuration du serveur web fonctionne mais est assez lourd. On peu > https://site.tld/htmgem/index.php?url=/url/page.gmi&style=<…> Il est aussi possible de faire une réécriture d’URL qui intègre le style : -> rewrite ^(.+\.gmi)$ /htmgem/?url=$1; -> rewrite ^(.+\.gmi)\ *\|\ *([\w_]+)$ /htmgem/?url=$1&style=$2; +> rewrite ^(.+\.gmi)$ /htmgem/index.php?rw=1&url=$1; +> rewrite ^(.+\.gmi)\ *\|\ *([\w_]+)$ /htmgem/index.php?rw=1&url=$1&style=$2; Par exemple, lorsque ce sera activé : => configuration-fr.gmi%20|%20pre Produit le code source de façon lisble à l’écran en utilisant la balise ****. => configuration-fr.gmi%20|%20source configuration-fr.gmi | source : Téléverse le code source. -=> ../css Sélecteur de style : la page contenant les styles permet de les essayer 😎 - ### Avec les styles déjà fournis… => configuration-fr.gmi%20|%20htmgem Style //HtmHem//, le style par défaut @@ -35,6 +34,8 @@ Par exemple, lorsque ce sera activé : => configuration-fr.gmi%20|%20raw Style //brut// => configuration-fr.gmi%20|%20debug Style //debug// +=> ../css Sélecteur de style : la page contenant les styles permet de les essayer 😎 + ## Décoration du texte La décoration du texte, qui interprête le **gras** par exemple, ne fait pas partie de la définition de GemText. La décoration du texte s’applique partout sauf sur les titres et blocs préformatés. diff --git a/docs/installation-en.gmi b/docs/installation-en.gmi index 07db762..b3a03de 100644 --- a/docs/installation-en.gmi +++ b/docs/installation-en.gmi @@ -18,16 +18,16 @@ To run HtmGem — after having copied the files — there is nothing to do but t ### Nginx ``` # This text must be placed in the server's configuration -index index.php index.gmi index.html -rewrite ^(.+\.gmi)$ htmgem/?url=$1; +index index.gmi index.php index.html +rewrite ^(.+\.gmi)$ htmgem/index.php?rw=1&url=$1; ``` ### Apache ``` # This text can be placed at the root of the web site in a file ".htaccess". -DirectoryIndex index.php index.gmi index.html +DirectoryIndex index.gmi index.php index.html RewriteEngine on -RewriteRule ^(.+\.gmi)$ htmgem/?url=$1 +RewriteRule ^(.+\.gmi)$ htmgem/index.php?rw=1&url=$1 ``` Afterwards, diff --git a/docs/installation-fr.gmi b/docs/installation-fr.gmi index 10c6e7d..798efb7 100644 --- a/docs/installation-fr.gmi +++ b/docs/installation-fr.gmi @@ -18,16 +18,16 @@ Pour faire fonctionner HtmGem — après avoir copié les fichiers — il ne res ### Nginx ``` # Ce texte doit être placé dans la configuration du serveur. -index index.php index.gmi index.html -rewrite ^(.+\.gmi)$ htmgem/?url=$1; +index index.gmi index.php index.html +rewrite ^(.+\.gmi)$ htmgem/index.php?rw=1&url=$1; ``` ### Apache ``` # Ce texte peut se placer à la racine du site dans un fichier ".htaccess". -DirectoryIndex index.php index.gmi index.html +DirectoryIndex index.gmi index.php index.html RewriteEngine on -RewriteRule ^(.+\.gmi)$ htmgem/?url=$1 +RewriteRule ^(.+\.gmi)$ htmgem/index.php?rw=1&url=$1 ``` Par la suite, diff --git a/index.php b/index.php index bfe1053..5c2d685 100644 --- a/index.php +++ b/index.php @@ -4,6 +4,7 @@ require_once "lib-htmgem.php"; # The url argument is always absolute compared to the document root. $url = @$_REQUEST["url"]; +$urlRewriting = @$_REQUEST["rw"]=="1"; /* Installation page * @@ -16,19 +17,25 @@ if (empty($url)) { http_response_code(403); die(""); } - $t = new \htmgem\GemTextTranslate_html(@file_get_contents("index.gmi")); + $t = new \htmgem\GemTextTranslate_html(@file_get_contents("index.gmi"), true, "/htmgem"); echo $t->getFullHtml(); exit(); } $documentRoot = $_SERVER['DOCUMENT_ROOT']; +if (!preg_match("/\.gmi$/", $url)) { + if ($url[-1] == "/") + $url = $url."index.gmi"; + else + $url = $url."/index.gmi"; +} + # Removes the headling and trailling slashes, to be sure there's not any. $filePath = rtrim($_SERVER['DOCUMENT_ROOT'], "/")."/".ltrim($url, "/"); switch(true) { case !realPath($filePath): - case !preg_match("/\.gmi$/", $url): # not finishing by .gmi case strpos($filePath, $documentRoot)!==0: # not in web directory $go404 = true; // Says 404 even if the file exists to not give any information. @@ -95,7 +102,11 @@ EOL; exit(); } -$t = new \htmgem\GemTextTranslate_html($fileContents, $textDecoration); +if ($urlRewriting) + $baseUrl = null; +else + $baseUrl = dirname($url); +$t = new \htmgem\GemTextTranslate_html($fileContents, $textDecoration, $baseUrl); if ("none" == $style) { $t->addCss(""); } elseif ("/" == @$style[0]) { diff --git a/lib-htmgem.php b/lib-htmgem.php index a471e63..104bf5b 100644 --- a/lib-htmgem.php +++ b/lib-htmgem.php @@ -182,7 +182,8 @@ class GemtextTranslate_html { protected $pageTitle = ""; public $translatedGemtext; - function __construct($parsedGemtext, $textDecoration=true) { + function __construct($parsedGemtext, $textDecoration=true, $baseUrl=Null) { + $this->baseUrl = $baseUrl; if (empty($parsedGemtext)) $parsedGemtext = ""; // to delete the last empty lines $parsedGemtext = rtrim($parsedGemtext); @@ -262,6 +263,23 @@ class GemtextTranslate_html { $text = preg_replace("/ +/", " ", $text); } + protected static function resolve_path($path) { + $absolute = "/"==$path[0]; + $parts = array_filter(explode("/", $path), 'strlen'); + $chuncks = array(); + foreach ($parts as $part) { + if ('.' == $part) continue; + if ('..' == $part) { + array_pop($chuncks); + } else { + $chuncks[] = $part; + } + } + $output = implode("/", $chuncks); + if ($absolute) $output = "/".$output; + return $output; + } + public function translate($textDecoration=true) { $output = ""; foreach ($this->parsedGemtext as $node) { @@ -316,7 +334,14 @@ class GemtextTranslate_html { } preg_match("/^([^:]+):/", $link, $matches); $protocol = @$matches[1]; - if (empty($protocol)) $protocol = "local"; + if (empty($protocol)) { + $protocol = "local"; + if (!is_null($this->baseUrl)) { // No URL rewriting + if ($link[0]!="/") $link = "{$this->baseUrl}/$link"; + $link = self::resolve_path($link); + $link = "/htmgem/index.php?url=$link"; + } + } $output .= "\n"; break; case "#":