Handles line feeds: Unix, Mac, Windows

This commit is contained in:
Christophe HENRY 2021-04-04 22:23:38 +02:00
parent cf54d98c61
commit 8cf174ecb3
8 changed files with 41 additions and 5 deletions

View File

@ -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) {

Binary file not shown.

View File

@ -0,0 +1,8 @@
<ul>
<li>unix
<li>mac
<li>dos
<li>unix
<li>mac
<li>dos
</ul>

View File

@ -0,0 +1,4 @@
* unix
* mac * dos
* unix
* mac * dos

View File

@ -0,0 +1,8 @@
<ul>
<li>unix
<li>mac
<li>dos
<li>unix
<li>mac
<li>dos
</ul>

View File

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

View File

@ -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"
);
}
}
}

View File

@ -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;
}
}