Mercurial > piecrust2
comparison piecrust/data/linker.py @ 866:d9059257743c
refactor: Make the linker work again.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Mon, 12 Jun 2017 22:10:50 -0700 |
parents | fddaf43424e2 |
children | 84fc72a17f7a |
comparison
equal
deleted
inserted
replaced
865:1bb0d973dc69 | 866:d9059257743c |
---|---|
22 'ancestors': '_debugRenderAncestors', | 22 'ancestors': '_debugRenderAncestors', |
23 'siblings': '_debugRenderSiblings', | 23 'siblings': '_debugRenderSiblings', |
24 'children': '_debugRenderChildren', | 24 'children': '_debugRenderChildren', |
25 'root': '_debugRenderRoot'} | 25 'root': '_debugRenderRoot'} |
26 | 26 |
27 def __init__(self, page): | 27 def __init__(self, source, content_item): |
28 self._page = page | 28 self._source = source |
29 self._content_item = page.content_item | 29 self._content_item = content_item |
30 self._source = page.source | |
31 self._app = page.app | |
32 | 30 |
33 self._parent = _unloaded | 31 self._parent_group = _unloaded |
34 self._ancestors = None | 32 self._ancestors = None |
35 self._siblings = None | 33 self._siblings = None |
36 self._children = None | 34 self._children = None |
37 | 35 |
38 @property | 36 @property |
39 def parent(self): | 37 def parent(self): |
40 if self._parent is _unloaded: | 38 a = self.ancestors |
41 pi = self._source.getRelatedContents(self._content_item, | 39 if a: |
42 REL_LOGICAL_PARENT_ITEM) | 40 return a[0] |
43 if pi is not None: | 41 return None |
44 pipage = self._app.getPage(self._source, pi) | |
45 self._parent = PaginationData(pipage) | |
46 else: | |
47 self._parent = None | |
48 return self._parent | |
49 | 42 |
50 @property | 43 @property |
51 def ancestors(self): | 44 def ancestors(self): |
52 if self._ancestors is None: | 45 if self._ancestors is None: |
53 cur_item = self._content_item | 46 self._ensureParentGroup() |
47 | |
48 src = self._source | |
49 app = src.app | |
50 | |
51 cur_group = self._parent_group | |
54 self._ancestors = [] | 52 self._ancestors = [] |
55 while True: | 53 while cur_group: |
56 pi = self._source.getRelatedContents( | 54 pi = src.getRelatedContents(cur_group, |
57 cur_item, REL_LOGICAL_PARENT_ITEM) | 55 REL_LOGICAL_PARENT_ITEM) |
58 if pi is not None: | 56 if pi is not None: |
59 pipage = self._app.getPage(self._source, pi) | 57 pipage = app.getPage(src, pi) |
60 self._ancestors.append(PaginationData(pipage)) | 58 self._ancestors.append(PaginationData(pipage)) |
61 cur_item = pi | 59 cur_group = src.getParentGroup(pi) |
62 else: | 60 else: |
63 break | 61 break |
64 return self._ancestors | 62 return self._ancestors |
65 | 63 |
66 @property | 64 @property |
67 def siblings(self): | 65 def siblings(self): |
68 if self._siblings is None: | 66 if self._siblings is None: |
67 self._ensureParentGroup() | |
68 | |
69 src = self._source | |
70 app = src.app | |
71 | |
69 self._siblings = [] | 72 self._siblings = [] |
70 parent_group = self._source.getParentGroup(self._content_item) | 73 for i in src.getContents(self._parent_group): |
71 for i in self._source.getContents(parent_group): | |
72 if not i.is_group: | 74 if not i.is_group: |
73 ipage = self._app.getPage(self._source, i) | 75 ipage = app.getPage(src, i) |
74 self._siblings.append(PaginationData(ipage)) | 76 self._siblings.append(PaginationData(ipage)) |
75 return self._siblings | 77 return self._siblings |
76 | 78 |
77 @property | 79 @property |
78 def children(self): | 80 def children(self): |
79 if self._children is None: | 81 if self._children is None: |
82 src = self._source | |
83 app = src.app | |
84 | |
80 self._children = [] | 85 self._children = [] |
81 child_group = self._source.getRelatedContents( | 86 child_group = src.getRelatedContents(self._content_item, |
82 self._content_item, REL_LOGICAl_CHILD_GROUP) | 87 REL_LOGICAl_CHILD_GROUP) |
83 if child_group: | 88 if child_group: |
84 for i in self._source.getContents(child_group): | 89 for i in src.getContents(child_group): |
85 ipage = self._app.getPage(self._source, i) | 90 ipage = app.getPage(src, i) |
86 self._children.append(PaginationData(ipage)) | 91 self._children.append(PaginationData(ipage)) |
87 return self._children | 92 return self._children |
88 | 93 |
94 def forpath(self, path): | |
95 # TODO: generalize this for sources that aren't file-system based. | |
96 item = self._source.findContent({'slug': path}) | |
97 return Linker(self._source, item) | |
98 | |
99 def childrenof(self, path): | |
100 # TODO: generalize this for sources that aren't file-system based. | |
101 src = self._source | |
102 app = src.app | |
103 group = src.findGroup(path) | |
104 if group is not None: | |
105 for i in src.getContents(group): | |
106 if not i.is_group: | |
107 ipage = app.getPage(src, i) | |
108 yield PaginationData(ipage) | |
109 return None | |
110 | |
111 def _ensureParentGroup(self): | |
112 if self._parent_group is _unloaded: | |
113 src = self._source | |
114 item = self._content_item | |
115 self._parent_group = src.getParentGroup(item) | |
116 | |
89 def _debugRenderAncestors(self): | 117 def _debugRenderAncestors(self): |
90 return [i.name for i in self.ancestors] | 118 return [i.title for i in self.ancestors] |
91 | 119 |
92 def _debugRenderSiblings(self): | 120 def _debugRenderSiblings(self): |
93 return [i.name for i in self.siblings] | 121 return [i.title for i in self.siblings] |
94 | 122 |
95 def _debugRenderChildren(self): | 123 def _debugRenderChildren(self): |
96 return [i.name for i in self.children] | 124 return [i.title for i in self.children] |
97 | 125 |
98 def _debugRenderRoot(self): | |
99 r = self.root | |
100 if r is not None: | |
101 return r.name | |
102 return None | |
103 |