Skip to content

Commit 9402456

Browse files
committed
correctly read annotations and notes with line breaks
1 parent 60e0ab5 commit 9402456

File tree

5 files changed

+60
-29
lines changed

5 files changed

+60
-29
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<p align="center">Convert your Kobo annotations to Readwise highlights</p>
44

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

6464
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.
6565

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

6868
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.
6969

bin/kobwise

100644100755
File mode changed.

src/Services/Converter.php

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function convert(ConversionData $data): string
4848
// The file's first line should be the title.
4949
$constants = [$this->readLine($source), $data->getAuthor(), $data->getUrl()];
5050

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

5353
$this->clerk->close($source);
5454
$this->clerk->close($target);
@@ -76,41 +76,36 @@ private function readLine(string $source): ?string
7676
}
7777

7878
/**
79-
* Process the next line of the file.
79+
* Parse and add the next highlight.
8080
*
81-
* @param string $source
82-
* @param string $target
83-
* @param array $constants
84-
* @param string|null $current
85-
* @param string|null $next
81+
* @param string $source
82+
* @param string $target
83+
* @param array $constants
8684
* @return void
8785
*/
88-
private function addRow(
89-
string $source,
90-
string $target,
91-
array $constants,
92-
?string $current = null,
93-
?string $next = null
94-
): void {
95-
$current = $next;
96-
97-
if (is_null($next = $this->readLine($source))) {
86+
private function processHighlight(string $source, string $target, array $constants): void
87+
{
88+
if (is_null($highlight = $this->readLine($source))) {
9889
return;
9990
}
10091

101-
if (empty($current)) {
102-
$this->addRow($source, $target, $constants, $current, $next);
92+
if (empty($highlight)) {
93+
$this->processHighlight($source, $target, $constants);
10394
return;
10495
}
10596

106-
$note = str_starts_with($next, 'Note: ') ? substr($next, 6) : null;
97+
$note = null;
10798

108-
$this->clerk->writeCsv($target, array_merge([$current, $note], $constants));
109-
110-
if (! empty($note)) {
111-
$next = $this->readLine($source);
99+
while (! empty($line = $this->readLine($source))) {
100+
if ($note !== null) {
101+
$note .= sprintf("\n%s", $line);
102+
} elseif (is_null($note = str_starts_with($line, 'Note: ') ? substr($line, 6) : null)) {
103+
$highlight .= sprintf("\n%s", $line);
104+
}
112105
}
113106

114-
$this->addRow($source, $target, $constants, $current, $next);
107+
$this->clerk->writeCsv($target, array_merge([$highlight, $note], $constants));
108+
109+
$this->processHighlight($source, $target, $constants);
115110
}
116111
}

tests/Commands/ConvertTest.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testItConvertsTheAnnotationsToHighlights()
2929

3030
fclose($resource);
3131

32-
$this->assertCount(10, $rows);
32+
$this->assertCount(9, $rows);
3333
$this->assertEquals(['Highlight', 'Note', 'Title', 'Author', 'URL'], $rows[0]);
3434
$this->assertEquals(
3535
[
@@ -42,6 +42,29 @@ public function testItConvertsTheAnnotationsToHighlights()
4242
$rows[1]
4343
);
4444

45+
$this->assertEquals(
46+
[
47+
'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.',
48+
'',
49+
'The Almanack of Naval Ravikant: A Guide to Wealth and Happiness',
50+
'Foo Bar',
51+
'https://foo.bar'
52+
],
53+
$rows[2]
54+
);
55+
56+
$this->assertEquals(
57+
[
58+
'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.
59+
If you look at even doctors who get rich (like really rich),',
60+
'',
61+
'The Almanack of Naval Ravikant: A Guide to Wealth and Happiness',
62+
'Foo Bar',
63+
'https://foo.bar'
64+
],
65+
$rows[4]
66+
);
67+
4568
$this->assertEquals(
4669
[
4770
'We’ve shared a lot of meals, shared a lot of deals, and hopped around the world together.',
@@ -50,6 +73,18 @@ public function testItConvertsTheAnnotationsToHighlights()
5073
'Foo Bar',
5174
'https://foo.bar'
5275
],
76+
$rows[6]
77+
);
78+
79+
$this->assertEquals(
80+
[
81+
'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...',
82+
'Test with note on
83+
multiple lines',
84+
'The Almanack of Naval Ravikant: A Guide to Wealth and Happiness',
85+
'Foo Bar',
86+
'https://foo.bar'
87+
],
5388
$rows[7]
5489
);
5590

tests/stubs/test.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ We’ve shared a lot of meals, shared a lot of deals, and hopped around the worl
1717
Note: Test with note and accentué
1818

1919
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...
20-
Note: Test with note
20+
Note: Test with note on
21+
multiple lines
2122

0 commit comments

Comments
 (0)