Skip to content

Commit

Permalink
Merge pull request #54 from shaarli/fix/keep-tab-characters
Browse files Browse the repository at this point in the history
Do not replace tab characters
  • Loading branch information
ArthurHoaro authored Jun 6, 2020
2 parents 407171f + 6dbb72a commit 47e3baa
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
6 changes: 3 additions & 3 deletions NetscapeBookmarkParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,15 +321,15 @@ public static function sanitizeString($bookmarkString)
// trim whitespace
$sanitized = trim($sanitized);

// trim carriage returns, replace tabs by a single space
$sanitized = str_replace(array("\r", "\t"), array('',' '), $sanitized);
// trim carriage returns
$sanitized = str_replace("\r", '', $sanitized);

// convert multiline descriptions to one-line descriptions
// line feeds are converted to <br>
$sanitized = preg_replace_callback(
'@<DD>(.*?)(</?(:?DT|DD|DL))@mis',
function($match) {
return '<DD>'.str_replace("\n", '<br>', trim($match[1])).PHP_EOL. $match[2];
return '<DD>'.str_replace("\n", '<br>', trim($match[1])).PHP_EOL. $match[2];
},
$sanitized
);
Expand Down
28 changes: 28 additions & 0 deletions tests/ParseShaarliWithTabsAndSpacesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Shaarli\NetscapeBookmarkParser;

class ParseShaarliWithTabsAndSpacesTest extends \PHPUnit_Framework_TestCase
{
/**
* Delete log file.
*/
public function tearDown()
{
@unlink(LoggerTestsUtils::getLogFile());
}

/**
* Parse flat Chromium bookmarks (no directories)
*/
public function testParseFlat()
{
$parser = new NetscapeBookmarkParser(false, null, '1');
$bkm = $parser->parseFile('tests/input/shaarli_with_tabs_and_spaces.htm');

static::assertSame(
trim(file_get_contents('tests/output/shaarli_with_tabs_and_spaces.txt')),
$bkm[0]['note']
);
}
}
27 changes: 27 additions & 0 deletions tests/input/shaarli_with_tabs_and_spaces.htm

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions tests/output/shaarli_with_tabs_and_spaces.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
I will write some formating using spaces:

* For example,
* lists,
+ but also
+ sublists
* use spaces.

Also code:

```
foreach ($data['linksToDisplay'] as $key => $bookmark) {
$length = strlen($bookmark['title']) + (342 * strlen($bookmark['description'])) / 836;
if (! empty($bookmark['thumbnail'])) {
$length += 100; // 1 thumbnails roughly takes 100 pixels height.
}
}
```

0 comments on commit 47e3baa

Please sign in to comment.