def test_info(fs): doc = """--- title: An Examination of the Navel created: 2019-06-04 10:12:13-08:00 keywords: - TrulyProfound ... #personal #book-draft # Preface: Reasons for #journaling As I have explained at length in [another note](../Another%20Note.md) and also published about online (see [this article](http://example.com/blah) among many others), ... """ path = '/fakenotes/test.md' fs.create_file(path, contents=doc) info = MarkdownAccessor(path).info() assert info.path == path assert info.links == [ LinkInfo(path, r) for r in sorted(['../Another%20Note.md', 'http://example.com/blah']) ] assert info.tags == { 'trulyprofound', 'personal', 'book-draft', 'journaling' } assert info.title == 'An Examination of the Navel' assert info.created == datetime(2019, 6, 4, 10, 12, 13, 0, timezone(timedelta(hours=-8)))
def test_meta_boundaries_in_body(fs): doc1 = """--- title: Hi ... keywords: - foo ... whatever """ doc2 = """--- title: Hi --- keywords: - foo --- whatever """ Path('/fakenotes').mkdir() for doc in [doc1, doc2]: path = '/fakenotes/doc.md' Path(path).write_text(doc) acc = MarkdownAccessor(path) info = acc.info() assert info.title == 'Hi' assert info.tags == set() acc.edit(AddTagCmd(path, 'testing')) acc.save() if doc == doc1: doc = doc.replace('\n...', '\n---', 1) doc = doc.replace('title', 'keywords:\n- testing\ntitle', 1) assert Path(path).read_text() == doc
def __init__(self, path: str): super().__init__(path) if path.endswith('.md'): self.accessor = MarkdownAccessor(path) elif path.endswith('.html'): self.accessor = HTMLAccessor(path) elif path.endswith('.pdf'): self.accessor = PDFAccessor(path) else: self.accessor = MiscAccessor(path)
def test_remove_hashtag(fs): doc = '#Tag1 tag1 #tag1. tag1#tag1 #tag1 #tag2 #tag1' # TODO Currently none of the whitespace around a tag is removed when the tag is, which can leave things # pretty ugly. But I'm not sure what the best approach is. expected = ' tag1 . tag1#tag1 #tag2 ' path = '/fakenotes/test.md' fs.create_file(path, contents=doc) acc = MarkdownAccessor(path) assert acc.info().tags == {'tag1', 'tag2'} acc.edit(DelTagCmd(path, 'tag1')) assert acc.save() assert Path(path).read_text() == expected
def test_change_metadata_tags(fs): doc = """--- keywords: - one - two ... text""" expected = """--- keywords: - three - two ... text""" path = '/fakenotes/test.md' fs.create_file(path, contents=doc) acc = MarkdownAccessor(path) acc.edit(AddTagCmd(path, 'THREE')) acc.edit(DelTagCmd(path, 'ONE')) assert acc.save() assert Path(path).read_text() == expected
def test_meta_body_separation(fs): # MarkdownAccessor does not preserve blank lines between meta and body. # This test formally recognizes this, and verifies our convention of # always separating meta and body by one blank line. # one blank line between meta and body expected = """--- title: new title --- body """ # no blank line between meta and body doc1 = """--- title: title --- body """ # one blank line between meta and body doc2 = """--- title: title --- body """ # two blank lines between meta and body doc3 = """--- title: title --- body """ Path('/fakenotes').mkdir() for doc in [doc1, doc2, doc3]: path = '/fakenotes/doc.md' Path(path).write_text(doc) acc = MarkdownAccessor(path) # change title to trigger a save acc.edit(SetTitleCmd(path, 'new title')) acc.save() assert Path(path).read_text() == expected
def test_ignore_fenced_code_blocks(fs): doc = """#tag1 [link](link1.md) ```foo #tag2 #tag3 text [link](link1.md) ``` #tag3 [link](link2.md) ``` [link](link3.md) ```""" path = '/fakenotes/text.md' fs.create_file(path, contents=doc) acc = MarkdownAccessor(path) info = acc.info() assert info.tags == {'tag1', 'tag3'} assert info.links == [LinkInfo(path, 'link1.md'), LinkInfo(path, 'link2.md')] acc.edit(ReplaceHrefCmd(path, 'link1.md', 'CHANGED1')) acc.edit(ReplaceHrefCmd(path, 'link2.md', 'CHANGED2')) acc.edit(ReplaceHrefCmd(path, 'link3.md', 'CHANGED3')) acc.edit(DelTagCmd(path, 'tag3')) acc.edit(DelTagCmd(path, 'tag2')) acc.save() assert Path(path).read_text() == """#tag1 [link](CHANGED1)
def test_change(fs): doc = """--- title: An Examination of the Navel --- #personal #book-draft # Preface: Reasons for #journaling As I have explained at length in [another note](../Another%20Note.md) and also published about online (see [this article](http://example.com/blahblah) and [this one](http://example.com/blah) among many others), ... """ expected = """--- created: 2019-06-04 10:12:13-08:00 title: A Close Examination of the Navel --- #personal #book-draft # Preface: Reasons for #journaling As I have explained at length in [another note](moved/another-note.md) and also published about online (see [this article](http://example.com/blahblah) and [this one](https://example.com/meh) among many others), ... """ path = '/fakenotes/test.md' fs.create_file(path, contents=doc) acc = MarkdownAccessor(path) acc.edit(ReplaceHrefCmd(path, '../Another%20Note.md', 'moved/another-note.md')) acc.edit(ReplaceHrefCmd(path, 'http://example.com/blah', 'https://example.com/meh')) acc.edit(SetTitleCmd(path, 'A Close Examination of the Navel')) acc.edit(SetCreatedCmd(path, datetime(2019, 6, 4, 10, 12, 13, 0, timezone(timedelta(hours=-8))))) assert acc.save() assert Path(path).read_text() == expected