mirror of
https://tildegit.org/sbgodin/HtmGem.git
synced 2023-08-25 13:53:12 +02:00
Handles line feeds: Unix, Mac, Windows
This commit is contained in:
parent
cf54d98c61
commit
8cf174ecb3
@ -12,7 +12,7 @@ mb_regex_encoding("UTF-8");
|
||||
function gemtextParser($fileContents) {
|
||||
if (empty($fileContents)) return array();
|
||||
$fileContents = rtrim($fileContents); // removes last empty line
|
||||
$fileLines = explode("\n", $fileContents);
|
||||
$fileLines = mb_split("\R", $fileContents); // Unix, Mac, Windows line feeds
|
||||
$mode = null;
|
||||
$current = array();
|
||||
foreach ($fileLines as $line) {
|
||||
|
BIN
tests/files_with_html/linefeeds-utf-16.txt
Normal file
BIN
tests/files_with_html/linefeeds-utf-16.txt
Normal file
Binary file not shown.
8
tests/files_with_html/linefeeds-utf-16.txt.html
Normal file
8
tests/files_with_html/linefeeds-utf-16.txt.html
Normal file
@ -0,0 +1,8 @@
|
||||
<ul>
|
||||
<li>unix
|
||||
<li>mac
|
||||
<li>dos
|
||||
<li>unix
|
||||
<li>mac
|
||||
<li>dos
|
||||
</ul>
|
4
tests/files_with_html/linefeeds.txt
Normal file
4
tests/files_with_html/linefeeds.txt
Normal file
@ -0,0 +1,4 @@
|
||||
* unix
|
||||
* mac
* dos
|
||||
* unix
|
||||
* mac
* dos
|
8
tests/files_with_html/linefeeds.txt.html
Normal file
8
tests/files_with_html/linefeeds.txt.html
Normal file
@ -0,0 +1,8 @@
|
||||
<ul>
|
||||
<li>unix
|
||||
<li>mac
|
||||
<li>dos
|
||||
<li>unix
|
||||
<li>mac
|
||||
<li>dos
|
||||
</ul>
|
@ -51,7 +51,7 @@ final class translateToGemtextTest extends TestCase {
|
||||
|
||||
#TODO: don't stop when problems are found, list all the faulty files
|
||||
public function test_translate_gemtext_files(): void {
|
||||
foreach(getGmiFiles(dirname(__FILE__)."/..") as $filePathname) {
|
||||
foreach(getFiles(dirname(__FILE__)."/..", "gmi") as $filePathname) {
|
||||
$fileContent = file_get_contents($filePathname);
|
||||
\htmgem\io\convertToUTF8($fileContent);
|
||||
$this->assertSame(
|
||||
|
@ -94,7 +94,7 @@ final class translateToHtmlTest extends TestCase {
|
||||
/** NOTE: the UTF-16 files must result in the same content as UTF-8 ones.
|
||||
* command to convert from UTF-8 to UTF-16: iconv -f utf8 -r utf16 text.gmi
|
||||
*/
|
||||
foreach(getGmiFiles(dirname(__FILE__)."/files_with_html") as $filePathname) {
|
||||
foreach(getFiles(dirname(__FILE__)."/files_with_html", "gmi") as $filePathname) {
|
||||
$fileContentGmi = file_get_contents($filePathname);
|
||||
\htmgem\io\convertToUTF8($fileContentGmi);
|
||||
$fileContentHtml = file_get_contents($filePathname.".html");
|
||||
@ -106,4 +106,20 @@ final class translateToHtmlTest extends TestCase {
|
||||
}
|
||||
}
|
||||
|
||||
public function test_line_feeds(): void {
|
||||
/** NOTE: the UTF-16 files must result in the same content as UTF-8 ones.
|
||||
* command to convert from UTF-8 to UTF-16: iconv -f utf8 -r utf16 text.gmi
|
||||
*/
|
||||
foreach(getFiles(dirname(__FILE__)."/files_with_html", "txt") as $filePathname) {
|
||||
$fileContentGmi = file_get_contents($filePathname);
|
||||
\htmgem\io\convertToUTF8($fileContentGmi);
|
||||
$fileContentHtml = file_get_contents($filePathname.".html");
|
||||
$this->assertSame(
|
||||
$fileContentHtml,
|
||||
translateHtml($fileContentGmi),
|
||||
"Line feeds, translation to HTML: $filePathname"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
function getGmiFiles($directory): generator {
|
||||
function getFiles($directory, $targetExtension): generator {
|
||||
$flags =
|
||||
FilesystemIterator::KEY_AS_PATHNAME
|
||||
| FilesystemIterator::CURRENT_AS_FILEINFO
|
||||
@ -14,7 +14,7 @@ function getGmiFiles($directory): generator {
|
||||
$filename = $fileinfo->getFilename();
|
||||
$filePathname = $fileinfo->getPathname();
|
||||
$extension = $fileinfo->getExtension();
|
||||
if ("gmi" == $extension) {
|
||||
if ($targetExtension == $extension) {
|
||||
yield $filePathname;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user