From e25e794dc480aaa93f855a23b3c48acd12fbfef9 Mon Sep 17 00:00:00 2001 From: ghost Date: Tue, 30 Jan 2024 11:46:07 +0200 Subject: [PATCH] initial commit --- .gitignore | 1 + README.md | 15 +++++++ composer.json | 14 ++++++ src/Dokuwiki.php | 108 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 src/Dokuwiki.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/README.md b/README.md index 59d4058..11e4901 100644 --- a/README.md +++ b/README.md @@ -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' + ) +); +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..ba906c9 --- /dev/null +++ b/composer.json @@ -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": {} +} diff --git a/src/Dokuwiki.php b/src/Dokuwiki.php new file mode 100644 index 0000000..3aa966b --- /dev/null +++ b/src/Dokuwiki.php @@ -0,0 +1,108 @@ + "# $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>/' => '```', + + '//' => '```', + '/]+)>/' => "$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 + ); + } +} \ No newline at end of file