Enables browsing without URL Rewriting

This commit is contained in:
Christophe HENRY 2021-03-28 21:50:36 +02:00
parent 733450cb35
commit 9471c43674
6 changed files with 59 additions and 21 deletions

View File

@ -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 **<pre>**.
=> 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.

View File

@ -1,4 +1,5 @@
# Configuration avancée
❗️ Cette page fonctionne avec la réécriture dURL activée ❗️
Par défaut, le même style est utilisé pour toutes les pages, //htmgem/css/htmgem.css//, sauf lorsquun fichier ayant le même nom que la page mais avec lextension **.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 dURL 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 **<pre>**.
=> 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 sapplique partout sauf sur les titres et blocs préformatés.

View File

@ -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,

View File

@ -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,

View File

@ -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("<!-- index.gmi missing -->");
}
$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]) {

View File

@ -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 .= "<p><a class='$protocol' href='$link'>$linkText</a></p>\n";
break;
case "#":