ghost
10 months ago
4 changed files with 138 additions and 0 deletions
@ -1,2 +1,17 @@
@@ -1,2 +1,17 @@
|
||||
# gemini-php |
||||
|
||||
PHP 8 Library for Gemini Protocol |
||||
|
||||
## DokuWiki |
||||
|
||||
### Convert |
||||
|
||||
``` |
||||
$dokuwiki = new \Yggverse\Gemini\Dokuwiki(); |
||||
|
||||
echo $dokuwiki->toGemini( |
||||
file_get_contents( |
||||
'data/pages/index.txt' |
||||
) |
||||
); |
||||
``` |
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
{ |
||||
"name": "yggverse/gemini", |
||||
"description": "PHP 8 Library for Gemini Protocol", |
||||
"keywords": [ "yggverse", "gemini", "wiki", "dokuwiki", "markdown" ], |
||||
"homepage": "https://github.com/yggverse/gemini-php", |
||||
"type": "library", |
||||
"license": "MIT", |
||||
"autoload": { |
||||
"psr-4": { |
||||
"Yggverse\\Gemini\\": "src/" |
||||
} |
||||
}, |
||||
"require": {} |
||||
} |
@ -0,0 +1,108 @@
@@ -0,0 +1,108 @@
|
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace Yggverse\Gemini; |
||||
|
||||
class Dokuwiki |
||||
{ |
||||
private array $_dictionary = |
||||
[ |
||||
// Headers |
||||
'/^[\s]?#([^#]+)/' => "# $1\n\r", |
||||
'/^[\s]?##([^#]+)/' => "## $1\n\r", |
||||
'/^[\s]?###([^#]+)/' => "### $1\n\r", |
||||
'/^[\s]?####([^#]+)/' => "### $1\n\r", |
||||
'/^[\s]?#####([^#]+)/' => "### $1\n\r", |
||||
'/^[\s]?######([^#]+)/' => "### $1\n\r", |
||||
|
||||
'/^[\s]?[=]{6}([^=]+)[=]{6}/' => "# $1\n\r", |
||||
'/^[\s]?[=]{5}([^=]+)[=]{5}/' => "## $1\n\r", |
||||
'/^[\s]?[=]{4}([^=]+)[=]{4}/' => "### $1\n\r", |
||||
'/^[\s]?[=]{3}([^=]+)[=]{3}/' => "### $1\n\r", |
||||
'/^[\s]?[=]{2}([^=]+)[=]{2}/' => "### $1\n\r", |
||||
'/^[\s]?[=]{1}([^=]+)[=]{1}/' => "### $1\n\r", |
||||
|
||||
// Links |
||||
// '/\{\{([^\:]+)\:([^\}\}]+)\}\}/' => "=> /$1 $1\n\r", // @TODO |
||||
'/\{\{indexmenu\>\:([^\}\}]+)\}\}/' => "=> /$1 $1\n\r", // @TODO |
||||
'/\[\[wp([A-z]{2})\>([^\|]+)\|([^\]\]]+)\]\]/' => "=> https://$1.wikipedia.org/wiki/$2 $3\n\r", |
||||
'/\[\[wp\>([^\|]+)\|([^\]\]]+)\]\]/' => "=> https://en.wikipedia.org/wiki/$1 $2\n\r", |
||||
'/\[\[([^|]+)\|([^\]\]]+)\]\]/' => "=> $1 $2\n\r", |
||||
|
||||
// Tags |
||||
'/<code>/' => '```', |
||||
'/<\/code>/' => '```', |
||||
|
||||
'/<file>/' => '```', |
||||
'/<file\s-\s([^>]+)>/' => "$1\n\r" . '```', |
||||
'/<\/file>/' => '```', |
||||
|
||||
'/[*]+([^*]+)[*]+/' => '*$1*', |
||||
'/[\'\']+([^\']+)[\'\']+/' => '```$1```', |
||||
|
||||
// List |
||||
'/^-/' => '* ', |
||||
|
||||
// Separators |
||||
'/[-]+/' => '-', |
||||
'/[ ]+/' => ' ', |
||||
'/[\\\]+/' => "\n\r", |
||||
'/[\n\r]{1,}/' => "\n\r", |
||||
]; |
||||
|
||||
public function __construct(?array $dictionary = null) |
||||
{ |
||||
if ($dictionary) |
||||
{ |
||||
$this->_dictionary = $dictionary; |
||||
} |
||||
} |
||||
|
||||
public function getDictionary(): array |
||||
{ |
||||
$this->_dictionary; |
||||
} |
||||
|
||||
public function setDictionary(array $dictionary) |
||||
{ |
||||
$this->_dictionary = $dictionary; |
||||
} |
||||
|
||||
public function getRule(string $key, string $value): ?string |
||||
{ |
||||
$this->_dictionary[$key] = isset($this->_dictionary[$key]) ? $value : null; |
||||
} |
||||
|
||||
public function setRule(string $key, string $value): void |
||||
{ |
||||
$this->_dictionary[$key] = $value; |
||||
} |
||||
|
||||
public function toGemini(string $data): string |
||||
{ |
||||
$lines = []; |
||||
|
||||
foreach ((array) explode(PHP_EOL, $data) as $line) |
||||
{ |
||||
$lines[] = trim( |
||||
preg_replace( |
||||
array_keys( |
||||
$this->_dictionary |
||||
), |
||||
array_values( |
||||
$this->_dictionary |
||||
), |
||||
trim( |
||||
$line |
||||
) |
||||
) |
||||
); |
||||
} |
||||
|
||||
return implode( |
||||
PHP_EOL, |
||||
$lines |
||||
); |
||||
} |
||||
} |
Loading…
Reference in new issue