Skip to content

Commit

Permalink
Merge pull request #34 from Laharah/master
Browse files Browse the repository at this point in the history
Add recursive directory searching. Skip non-text files.
  • Loading branch information
A authored Jul 31, 2022
2 parents 4a22415 + 678c298 commit ffd1740
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 37 deletions.
3 changes: 1 addition & 2 deletions src/blog/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ def get_all(layouts_dir):
layouts = {}

for file in fs.get_files_in_dir(layouts_dir):
path = os.path.join(layouts_dir, file)
layout = Layout(path)
layout = Layout(file)
layouts[layout.name] = layout

return layouts
Expand Down
30 changes: 15 additions & 15 deletions src/lib/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
def change_ext(ext: str, file_path: str):
"""Changes extension in path"""
pre, _ = os.path.splitext(file_path)
return pre + '.' + ext
return pre + "." + ext


def rm_dir(directory: str):
Expand All @@ -32,16 +32,16 @@ def copy_dir(source: str, dest: str):


def get_files_in_dir(dir: str, filter_partials=False):
"""returns files from given dir with optional partials filtering"""
file_names = [
f for f in os.listdir(dir) if os.path.isfile(os.path.join(dir, f))
]

if filter_partials:
file_names = filter(
lambda f: not f.startswith('_', 0, -1),
file_names,
)
"""recursively returns files from given dir with optional partials filtering"""
file_names = []
for root, dirs, files in os.walk(dir):
dirs[:] = [f for f in dirs if not f.startswith(".")]
if filter_partials:
files = filter(
lambda f: not f.startswith("_", 0, -1),
files,
)
file_names.extend(os.path.join(root, f) for f in files)

return file_names

Expand All @@ -57,7 +57,7 @@ def copy_file(src: str, dest: str):

def write_file(dest, content):
os.makedirs(os.path.dirname(dest), exist_ok=True)
with open(dest, 'a') as f:
with open(dest, "a") as f:
print(content, file=f)


Expand All @@ -71,11 +71,11 @@ def load(filename):
f = frontmatter.load(filename)
return [filename, f.metadata, f.content]
except Exception as error:
print(f'[ERROR] There is an error loading {filename}: {error}')
exit(1)
print(f"[ERROR] There is an error loading {filename}: {error}")
raise


def normalize_path(path: str):
if path[0] == '/':
if path[0] == "/":
return os.path.realpath(path)
return path
26 changes: 12 additions & 14 deletions src/obsidian/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,19 @@ class Page:
def __init__(self, data: ContentData):
self.data = data
self.head = self.build_tree()
self.data.entities = list(
map(TreeNode.unwrap, self.head.flat_children())
)
self.data.entities = list(map(TreeNode.unwrap, self.head.flat_children()))

@staticmethod
def get_all(pages_dir):
pages = []

for file in fs.get_files_in_dir(pages_dir):
path = os.path.join(pages_dir, file)
filename, meta, content = fs.load(path)
for file in fs.get_files_in_dir(pages_dir, filter_partials=True):
try:
filename, meta, content = fs.load(file)
except UnicodeDecodeError as e:
continue

content_data = ContentData(
filename=filename, meta=meta, content=content
)
content_data = ContentData(filename=filename, meta=meta, content=content)

page = Page(content_data)
pages.append(page)
Expand All @@ -67,7 +65,7 @@ def render(self, context=None):
if context == None:
context = {}
content = self.data.content
if self.data.ext == '.md':
if self.data.ext == ".md":
content = self.render_entities()
content = markdown.render(content)
try:
Expand All @@ -77,13 +75,13 @@ def render(self, context=None):

def render_entities(self):
for entity in self.data.entities:
if hasattr(entity, 'render'):
if hasattr(entity, "render"):
data = entity.data
if hasattr(data, 'is_private') and data.is_private:
if data.content != '':
if hasattr(data, "is_private") and data.is_private:
if data.content != "":
print(data.content)
print(
f' - [SKIP]: {data.placeholder} is private, add `published: True` attribute to the frontmetter to publish it'
f" - [SKIP]: {data.placeholder} is private, add `published: True` attribute to the frontmetter to publish it"
)
continue
self.data.content = entity.render(self.data)
Expand Down
4 changes: 4 additions & 0 deletions tests/__fixtures__/page-nested-dirs/page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
published: True
---
[[subnote]]
4 changes: 4 additions & 0 deletions tests/__fixtures__/page-nested-dirs/sub_dir/subnote.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
published: True
---
[[subsubnote]]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
published: True
---

the deepest note
30 changes: 24 additions & 6 deletions tests/obsidian/page_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@


@pytest.mark.parametrize(
'fixture_name',
"fixture_name",
[
('page_nested'),
("page_nested"),
],
)
def test_page_nested(snapshot, fixture_name):
cwd = os.getcwd()
fixture_path = get_fixture_path(fixture_name)
page_path = f'{fixture_path}/page.md'
page_path = f"{fixture_path}/page.md"
os.chdir(fixture_path)

page = create_page(*fs.load(page_path))

snapshot.assert_match(page.render(), f'{fixture_name}.html')
snapshot.assert_match(page.render(), f"{fixture_name}.html")
os.chdir(cwd)


def test_page_get_all():
cwd = os.getcwd()
fixture_path = get_fixture_path('pages_dir')
fixture_path = get_fixture_path("pages_dir")
os.chdir(fixture_path)
pages_dir = 'Pages'
pages_dir = "Pages"

pages = Page.get_all(pages_dir)

Expand All @@ -36,3 +36,21 @@ def test_page_get_all():
assert page.data.content == page.data.filename

os.chdir(cwd)


@pytest.mark.parametrize(
"fixture_name",
[
("page-nested-dirs"),
],
)
def test_page_nested_dir(snapshot, fixture_name):
cwd = os.getcwd()
fixture_path = get_fixture_path(fixture_name)
os.chdir(fixture_path)

pages = Page.get_all(fixture_path)

assert len(pages) == 3

os.chdir(cwd)

0 comments on commit ffd1740

Please sign in to comment.