mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-08 05:39:13 +00:00
Normalize org notes path to be relative to home directory
- This is still clunky but it should be commitable - General enough that it'll work even when a users notes are not in the home directory - While solving for the special case where: - Notes are being processed on a different machine and used on a different machine - But the notes directory is in the same location relative to home on both the machines
This commit is contained in:
@@ -35,6 +35,13 @@ constructing data structures of these classes.
|
|||||||
|
|
||||||
import re, sys
|
import re, sys
|
||||||
import datetime
|
import datetime
|
||||||
|
from pathlib import Path
|
||||||
|
from os.path import relpath
|
||||||
|
|
||||||
|
def normalize_filename(filename):
|
||||||
|
file_relative_to_home = f'~/{relpath(filename, start=Path.home())}'
|
||||||
|
escaped_filename = f'{file_relative_to_home}'.replace("[","\[").replace("]","\]")
|
||||||
|
return escaped_filename
|
||||||
|
|
||||||
def makelist(filename):
|
def makelist(filename):
|
||||||
"""
|
"""
|
||||||
@@ -81,7 +88,7 @@ def makelist(filename):
|
|||||||
deadline_date = ''
|
deadline_date = ''
|
||||||
thisNode.setProperties(propdict)
|
thisNode.setProperties(propdict)
|
||||||
nodelist.append( thisNode )
|
nodelist.append( thisNode )
|
||||||
propdict = {'LINE': f'file:{filename}::{ctr}'}
|
propdict = {'LINE': f'file:{normalize_filename(filename)}::{ctr}'}
|
||||||
level = hdng.group(1)
|
level = hdng.group(1)
|
||||||
heading = hdng.group(2)
|
heading = hdng.group(2)
|
||||||
bodytext = ""
|
bodytext = ""
|
||||||
@@ -175,9 +182,8 @@ def makelist(filename):
|
|||||||
n.setHeading(prtysrch.group(2))
|
n.setHeading(prtysrch.group(2))
|
||||||
|
|
||||||
# Set SOURCE property to a file+heading based org-mode link to the entry
|
# Set SOURCE property to a file+heading based org-mode link to the entry
|
||||||
escaped_filename = f'{filename}'.replace("[","\[").replace("]","\]")
|
|
||||||
escaped_heading = n.Heading().replace("[","\[").replace("]","\]")
|
escaped_heading = n.Heading().replace("[","\[").replace("]","\]")
|
||||||
n.properties['SOURCE'] = f'[[file:{escaped_filename}::*{escaped_heading}]]'
|
n.properties['SOURCE'] = f'[[file:{normalize_filename(filename)}::*{escaped_heading}]]'
|
||||||
|
|
||||||
return nodelist
|
return nodelist
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
# Standard Packages
|
# Standard Packages
|
||||||
import datetime
|
import datetime
|
||||||
from operator import contains
|
from os.path import relpath
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
# Internal Packages
|
# Internal Packages
|
||||||
from src.processor.org_mode import orgnode
|
from src.processor.org_mode import orgnode
|
||||||
@@ -78,17 +79,18 @@ Body Line 1
|
|||||||
Body Line 2
|
Body Line 2
|
||||||
'''
|
'''
|
||||||
orgfile = create_file(tmp_path, entry)
|
orgfile = create_file(tmp_path, entry)
|
||||||
|
normalized_orgfile = f'~/{relpath(orgfile, start=Path.home())}'
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
entries = orgnode.makelist(orgfile)
|
entries = orgnode.makelist(orgfile)
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
# SOURCE link rendered with Heading
|
# SOURCE link rendered with Heading
|
||||||
assert f':SOURCE: [[file:{orgfile}::*{entries[0].Heading()}]]' in f'{entries[0]}'
|
assert f':SOURCE: [[file:{normalized_orgfile}::*{entries[0].Heading()}]]' in f'{entries[0]}'
|
||||||
# ID link rendered with ID
|
# ID link rendered with ID
|
||||||
assert f':ID: id:123-456-789-4234-1231' in f'{entries[0]}'
|
assert f':ID: id:123-456-789-4234-1231' in f'{entries[0]}'
|
||||||
# LINE link rendered with line number
|
# LINE link rendered with line number
|
||||||
assert f':LINE: file:{orgfile}::2' in f'{entries[0]}'
|
assert f':LINE: file:{normalized_orgfile}::2' in f'{entries[0]}'
|
||||||
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------------------------------
|
# ----------------------------------------------------------------------------------------------------
|
||||||
@@ -111,7 +113,8 @@ Body Line 1'''
|
|||||||
# parsed heading from entry
|
# parsed heading from entry
|
||||||
assert entries[0].Heading() == "Heading[1]"
|
assert entries[0].Heading() == "Heading[1]"
|
||||||
# ensure SOURCE link has square brackets in filename, heading escaped in rendered entries
|
# ensure SOURCE link has square brackets in filename, heading escaped in rendered entries
|
||||||
escaped_orgfile = f'{orgfile}'.replace("[1]", "\\[1\\]")
|
normalized_orgfile = f'~/{relpath(orgfile, start=Path.home())}'
|
||||||
|
escaped_orgfile = f'{normalized_orgfile}'.replace("[1]", "\\[1\\]")
|
||||||
assert f':SOURCE: [[file:{escaped_orgfile}::*Heading\[1\]' in f'{entries[0]}'
|
assert f':SOURCE: [[file:{escaped_orgfile}::*Heading\[1\]' in f'{entries[0]}'
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user