+import markdown::wikilinks
+import ordered_tree
+
+redef class Nitiwiki
+ # Looks up a WikiEntry by its `name`.
+ #
+ # Rules are:
+ # 1. Looks in the current section
+ # 2. Looks in the current section children
+ # 3. Looks in the current section parent
+ # 4. Looks up to wiki root
+ #
+ # Returns `null` if no article can be found.
+ fun lookup_entry_by_name(context: WikiEntry, name: String): nullable WikiEntry do
+ var section: nullable WikiEntry = context.parent or else context
+ var res = section.lookup_entry_by_name(name)
+ if res != null then return res
+ while section != null do
+ if section.name == name then return section
+ if section.children.has_key(name) then return section.children[name]
+ section = section.parent
+ end
+ return null
+ end
+
+ # Looks up a WikiEntry by its `title`.
+ #
+ # Rules are:
+ # 1. Looks in the current section
+ # 2. Looks in the current section children
+ # 3. Looks in the current section parent
+ # 4. Looks up to wiki root
+ #
+ # Returns `null` if no article can be found.
+ fun lookup_entry_by_title(context: WikiEntry, title: String): nullable WikiEntry do
+ var section: nullable WikiEntry = context.parent or else context
+ var res = section.lookup_entry_by_title(title)
+ if res != null then return res
+ while section != null do
+ if section.title.to_lower == title.to_lower then return section
+ for child in section.children.values do
+ if child.title.to_lower == title.to_lower then return child
+ end
+ section = section.parent
+ end
+ return null
+ end
+
+ # Looks up a WikiEntry by its `path`.
+ #
+ # Path can be relative from `context` like `context/entry`.
+ # Or absolute like `/entry1/entry2`.
+ #
+ # Returns `null` if no article can be found.
+ fun lookup_entry_by_path(context: WikiEntry, path: String): nullable WikiEntry do
+ var entry = context.parent or else context
+ var parts = path.split_with("/")
+ if path.has_prefix("/") then
+ entry = root_section
+ if parts.is_empty then return root_section.index
+ parts.shift
+ end
+ while not parts.is_empty do
+ var name = parts.shift
+ if name.is_empty then continue
+ if entry.name == name then continue
+ if not entry.children.has_key(name) then return null
+ entry = entry.children[name]
+ end
+ return entry
+ end
+
+ # Trails between pages
+ #
+ # Trails are represented as a forest of entries.
+ # This way it is possible to represent a flat-trail as a visit of a tree.
+ var trails = new OrderedTree[WikiEntry]
+end