Skip to content

Commit

Permalink
Merge pull request #1 from osteel/bugfix/line-breaks
Browse files Browse the repository at this point in the history
Correctly read annotations and notes with line breaks
  • Loading branch information
osteel authored Nov 28, 2021
2 parents 60e0ab5 + 9402456 commit 2b97e6c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 29 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<p align="center">Convert your Kobo annotations to Readwise highlights</p>

<p align="center">
<img height="400" alt="Preview" src="/art/preview.png">
<img alt="Preview" src="/art/preview.png">
<p align="center">
<a href="https://github.com/osteel/kobwise/actions"><img alt="Build Status" src="https://github.com/osteel/kobwise/workflows/CI/badge.svg"></a>
<a href="//packagist.org/packages/osteel/kobwise"><img alt="Latest Stable Version" src="https://poser.pugx.org/osteel/kobwise/v"></a>
Expand Down Expand Up @@ -63,7 +63,7 @@ composer global remove osteel/kobwise

Kobo's annotation files are formatted in such a way that it is not easy to distinguish separate annotations from different paragraphs belonging to the same annotation.

It is therefore recommended to skim through the annotation file beforehand, and to delete any line break between paragraphs that should belong together.
It is therefore recommended to skim through the annotation file beforehand, and to delete any empty lines between paragraphs that should belong together.

As Readwise doesn't allow its users to [permanently delete highlights](https://help.readwise.io/article/123-why-cant-i-permanently-delete-highlights), it is also recommended to review the `.csv` file before import.

Expand Down
Empty file modified bin/kobwise
100644 → 100755
Empty file.
45 changes: 20 additions & 25 deletions src/Services/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function convert(ConversionData $data): string
// The file's first line should be the title.
$constants = [$this->readLine($source), $data->getAuthor(), $data->getUrl()];

$this->addRow($source, $target, $constants);
$this->processHighlight($source, $target, $constants);

$this->clerk->close($source);
$this->clerk->close($target);
Expand Down Expand Up @@ -76,41 +76,36 @@ private function readLine(string $source): ?string
}

/**
* Process the next line of the file.
* Parse and add the next highlight.
*
* @param string $source
* @param string $target
* @param array $constants
* @param string|null $current
* @param string|null $next
* @param string $source
* @param string $target
* @param array $constants
* @return void
*/
private function addRow(
string $source,
string $target,
array $constants,
?string $current = null,
?string $next = null
): void {
$current = $next;

if (is_null($next = $this->readLine($source))) {
private function processHighlight(string $source, string $target, array $constants): void
{
if (is_null($highlight = $this->readLine($source))) {
return;
}

if (empty($current)) {
$this->addRow($source, $target, $constants, $current, $next);
if (empty($highlight)) {
$this->processHighlight($source, $target, $constants);
return;
}

$note = str_starts_with($next, 'Note: ') ? substr($next, 6) : null;
$note = null;

$this->clerk->writeCsv($target, array_merge([$current, $note], $constants));

if (! empty($note)) {
$next = $this->readLine($source);
while (! empty($line = $this->readLine($source))) {
if ($note !== null) {
$note .= sprintf("\n%s", $line);
} elseif (is_null($note = str_starts_with($line, 'Note: ') ? substr($line, 6) : null)) {
$highlight .= sprintf("\n%s", $line);
}
}

$this->addRow($source, $target, $constants, $current, $next);
$this->clerk->writeCsv($target, array_merge([$highlight, $note], $constants));

$this->processHighlight($source, $target, $constants);
}
}
37 changes: 36 additions & 1 deletion tests/Commands/ConvertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testItConvertsTheAnnotationsToHighlights()

fclose($resource);

$this->assertCount(10, $rows);
$this->assertCount(9, $rows);
$this->assertEquals(['Highlight', 'Note', 'Title', 'Author', 'URL'], $rows[0]);
$this->assertEquals(
[
Expand All @@ -42,6 +42,29 @@ public function testItConvertsTheAnnotationsToHighlights()
$rows[1]
);

$this->assertEquals(
[
'It’s ownership versus wage work. If you are paid for renting out your time, even lawyers and doctors, you can make some money, but you’re not going to make the money that gives you financial freedom.',
'',
'The Almanack of Naval Ravikant: A Guide to Wealth and Happiness',
'Foo Bar',
'https://foo.bar'
],
$rows[2]
);

$this->assertEquals(
[
'Without ownership, when you’re sleeping, you’re not earning. When you’re retired, you’re not earning. When you’re on vacation, you’re not earning. And you can’t earn nonlinearly.
If you look at even doctors who get rich (like really rich),',
'',
'The Almanack of Naval Ravikant: A Guide to Wealth and Happiness',
'Foo Bar',
'https://foo.bar'
],
$rows[4]
);

$this->assertEquals(
[
'We’ve shared a lot of meals, shared a lot of deals, and hopped around the world together.',
Expand All @@ -50,6 +73,18 @@ public function testItConvertsTheAnnotationsToHighlights()
'Foo Bar',
'https://foo.bar'
],
$rows[6]
);

$this->assertEquals(
[
'He can be as blunt as a foot to the face, but that’s part of what I love and respect about him: you never have to guess what Naval is thinking. I’ve never had to guess how he’s feeling about me, someone else, or a situation. This is a huge relief in a world of double-talk an...',
'Test with note on
multiple lines',
'The Almanack of Naval Ravikant: A Guide to Wealth and Happiness',
'Foo Bar',
'https://foo.bar'
],
$rows[7]
);

Expand Down
3 changes: 2 additions & 1 deletion tests/stubs/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ We’ve shared a lot of meals, shared a lot of deals, and hopped around the worl
Note: Test with note and accentué

He can be as blunt as a foot to the face, but that’s part of what I love and respect about him: you never have to guess what Naval is thinking. I’ve never had to guess how he’s feeling about me, someone else, or a situation. This is a huge relief in a world of double-talk an...
Note: Test with note
Note: Test with note on
multiple lines

0 comments on commit 2b97e6c

Please sign in to comment.