-
Notifications
You must be signed in to change notification settings - Fork 0
/
2
99 lines (73 loc) · 2.62 KB
/
2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import logging
from sys import argv
from render_engine.cli.cli import get_app, split_module_site
from render_engine.collection import Collection
from render_engine.page import Page
from textual.app import ComposeResult
from textual.widget import Widget
from textual.widgets import (
Label,
ListItem,
ListView,
Markdown,
)
from editor import Editor, EditorScreen
module, app = split_module_site(argv[1])
site = get_app(module, app)
class FileLabel(Label):
def __init__(self, file: Page):
super().__init__(file.title)
self.content = file.content
def files_in_collection(collection: Collection) -> list[ListItem]:
"""Get a list of the files in a collection"""
return [
ListItem(
FileLabel(file),
name=file.__class__.__name__,
)
for file in collection.sorted_pages
]
class CollectionLabel(Label):
def __init__(self, collection: Collection):
super().__init__(collection.__class__.__name__)
self.collection = collection
def get_collections(site):
"""Get a list of the collections from the site route_list"""
return [
collection
for _, collection in site.route_list.items()
if isinstance(collection, Collection)
]
def get_collection_labels(collections: list[Collection]) -> list[ListItem]:
"""Get a list of the collections from the site route_list"""
return [
ListItem(
CollectionLabel(
collection,
),
name=collection.__class__.__name__,
id=collection.__class__.__name__,
)
for collection in collections
]
class CollectionList(ListView):
"""A list of collections"""
COMPONENT_CLASSES = {"listview-sidebar"}
class CollectionFiles(ListView):
"""A widget for displaying the files in a collection"""
COMPONENT_CLASSES = {"listview-sidebar"}
BINDINGS = [("e", "edit_file")]
async def action_edit_file(self) -> None:
if self.highlighted_child is not None:
content = self.highlighted_child.children[0].content
await self.app.push_screen("editor")
self.app.query_one("#file_editor").load_text(content)
class CollectionSideBar(Widget):
"""A sidebar for the collections"""
def compose(self) -> ComposeResult:
collections = get_collections(site)
yield CollectionList(*get_collection_labels(collections), id="collection_list")
yield CollectionFiles(*files_in_collection(collections[0]), id="file_selection")
class MarkdownPreview(Markdown):
"""Custom Markdown widget"""
COMPONENT_CLASSES = {"editor": EditorScreen}