MkTimeline

github Show source on GitHub

Node to show a JavaScript-supported Timeline.

Description

Consists of cards which slide in and out once they enter / leave the screen.

Example: From TOML format

Jinja

{{
"
['Step 1']
title = 'test'
content = 'Some **markdown**'
image = 'https://picsum.photos/400'
['Step 2']
title = 'test'
content = 'Some **markdown**'
image = 'https://picsum.photos/401'

" | load_toml | MkTimeline
}}

Python

MkTimeline([...])

test

Some markdown

test

Some markdown

<section class="timeline">
<div class="timeline-item">
<div class="timeline-img"></div>
<div class="timeline-content timeline-card js--fadeInLeft">
<div style="background: linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.4)), url(https://picsum.photos/400) center center no-repeat; background-size: cover;" class="timeline-img-header">
<p>
<h2>test</h2>
</p>
</div>
<p>
<p>Some <strong>markdown</strong>
</p>
</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-img"></div>
<div class="timeline-content timeline-card js--fadeInRight">
<div style="background: linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.4)), url(https://picsum.photos/401) center center no-repeat; background-size: cover;" class="timeline-img-header">
<p>
<h2>test</h2>
</p>
</div>
<p>
<p>Some <strong>markdown</strong>
</p>
</p>
</div>
</div>
</section>
<section class="timeline">
<div class="timeline-item">
<div class="timeline-img"></div>
<div class="timeline-content timeline-card js--fadeInLeft">
<div style="background: linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.4)), url(https://picsum.photos/400) center center no-repeat; background-size: cover;" class="timeline-img-header">
<p>
<h2>test</h2>
</p>
</div>
<p>
<p>Some <strong>markdown</strong>
</p>
</p>
</div>
</div>
<div class="timeline-item">
<div class="timeline-img"></div>
<div class="timeline-content timeline-card js--fadeInRight">
<div style="background: linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.4)), url(https://picsum.photos/401) center center no-repeat; background-size: cover;" class="timeline-img-header">
<p>
<h2>test</h2>
</p>
</div>
<p>
<p>Some <strong>markdown</strong>
</p>
</p>
</div>
</div>
</section>
MkTimeline
├── MkTimelineItem('test', None, image='https://picsum.photos/400')
│   ╰── MkText('Some **markdown**')
╰── MkTimelineItem('test', None, image='https://picsum.photos/401')
    ╰── MkText('Some **markdown**')

class MkTimeline(mkcontainer.MkContainer)

Name

Children

Inherits

MkContainer
mknodes.basenodes.mkcontainer
A node containing other MkNodes.

graph TD
  1587059980["mktimeline.MkTimeline"]
  835538629["mkcontainer.MkContainer"]
  1956333208["mkcontainer.MkContainerBase"]
  1359323199["mknode.MkNode"]
  2751680898["builtins.object"]
  835538629 --> 1587059980
  1956333208 --> 835538629
  1359323199 --> 1956333208
  2751680898 --> 1359323199
/home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mktimeline/metadata.toml
[metadata]
icon = "mdi:timeline"
name = "MkTimeline"

[[resources.css]]
filename = "timeline.css"

[[resources.js]]
is_library = true
link = "https://cdn.jsdelivr.net/npm/scrollreveal@3.4.0/dist/scrollreveal.min.js"

[[resources.js]]
link = "https://code.jquery.com/jquery-2.2.4.min.js"
is_library = true

[[resources.js]]
filename = "timeline.js"

[examples.toml]
title = "From TOML format"
jinja = """
{{
"
['Step 1']
title = 'test'
content = 'Some **markdown**'
image = 'https://picsum.photos/400'
['Step 2']
title = 'test'
content = 'Some **markdown**'
image = 'https://picsum.photos/401'

" | load_toml | MkTimeline
}}
"""

[fragments]
item = """
<div class="timeline-item">
<div class="timeline-img"></div>
<div class="timeline-content{{ " timeline-card" if node.image else "" }} js--fadeIn{{ node.fade_direction | capitalize }}">
{% if node.image %}
<div style="background: linear-gradient(rgba(0, 0, 0, 0), rgba(0, 0, 0, 0.4)), url({{ node.image }}) center center no-repeat; background-size: cover;" class="timeline-img-header">
<p>
<h2>{{ node.title }}</h2>
</p>
</div>
{% else %}
<h2>{{ node.title }}</h2>
{% endif %}
{% if node.label %}
<div class="date">{{ node.label }}</div>
{% endif %}
<p>
{{ node.content | to_html }}
</p>
{{ node.button_text | html_link(node.link, class_="bnt-more") }}
</div>
</div>
"""

# [output.markdown]
# template = """

# <section class="timeline">
# {% for item in node.get_items() %}
# {{ "fragments/item" | render_template(node=item) }}
# {% endfor %}
# </section>

# """
mknodes.basenodes.mktimeline.MkTimeline
 97class MkTimeline(mkcontainer.MkContainer):
 98    """Node to show a JavaScript-supported Timeline.
 99
100    Consists of cards which slide in and out once they enter / leave the screen.
101    """
102
103    ICON = "material/timeline"
104    JS_FILES = [
105        resources.JSFile(JQUERY_LINK, is_library=True),
106        resources.JSFile(SCROLLREVEAL_LINK, is_library=True),
107        resources.JSText(SCRIPT, "scrollreveal.js"),  # type: ignore[list-item]
108        resources.JSFile("timeline.js"),
109    ]
110    CSS = [resources.CSSFile("timeline.css")]
111
112    def __init__(
113        self,
114        items: list[Any] | str | dict[str, Any] | os.PathLike[str] | None = None,
115        **kwargs: Any,
116    ) -> None:
117        """Constructor.
118
119        Args:
120            items: Timeline items or a path to a TOML file containing timeline data
121            kwargs: Keyword arguments passed to parent
122        """
123        if isinstance(items, str | os.PathLike):
124            text = pathhelpers.load_file_cached(str(items))
125            data = tomllib.loads(text)
126            items = [MkTimelineItem(**step) for step in data.values()]
127        elif isinstance(items, dict):
128            items = [MkTimelineItem(**step) for step in items.values()]
129        super().__init__(items, **kwargs)
130
131    def get_items(self) -> list[MkTimelineItem]:  # type: ignore[override]
132        """Return the list of timeline items."""
133        return self._items  # type: ignore[return-value]
134
135    def get_element(self) -> xml.Section:
136        root = xml.Section("timeline")
137        for i, item in enumerate(self.get_items()):
138            item.fade_direction = "left" if i % 2 == 0 else "right"
139            elem = item.get_element()
140            root.append(elem)
141        return root
142
143    def to_md_unprocessed(self) -> str:
144        root = self.get_element()
145        return "\n\n" + root.to_string(space="") + "\n\n"
146
147    def add_item(
148        self,
149        title: str = "",
150        content: str | mknode.MkNode = "",
151        *,
152        label: str = "",
153        link: str = "",
154        button_text: str = "More",
155        image: str = "",
156        **kwargs: Any,
157    ):
158        """Add a timeline item.
159
160        title: Item header
161        content: Markdown for content
162        label: Label to be displayed in small box at the top
163        link: Optional button-link. Text of button can be set via button_text
164        button_text: Text for the link button.
165        image: Optional image for the item
166        kwargs: keyword arguments passed to parent
167        """
168        item = MkTimelineItem(
169            title=title,
170            content=content,
171            label=label,
172            link=link,
173            button_text=button_text,
174            image=image,
175            **kwargs,
176        )
177        self += item
178        return item
Page info
Code for this page
mknodes.manual.nodes_section.create_section_for_nodes
 93def create_section_for_nodes(
 94    nav: mk.MkNav,
 95    klasses: Sequence[type[mk.MkNode]],
 96) -> mk.MkTable:
 97    table = mk.MkTable(columns=["Node", "Docstrings", "Markdown extensions"])
 98    for kls in klasses:
 99        tpl = "classpage_custom.jinja"
100        page = mk.MkClassPage(kls, icon=kls.ICON, template_path=tpl, parent=nav, hide="toc")
101        nav += page
102        if kls.STATUS:
103            page.metadata.status = kls.STATUS
104        elif kls.JS_FILES:
105            page.metadata.status = "js"
106        elif kls.CSS:
107            page.metadata.status = "css"
108        page.created_by = create_section_for_nodes
109        link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
110        extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
111        table.add_row((link, kls.__doc__, extensions))
112    return table
Resources
Resources(css=[],
          markdown_extensions={'admonition': {},
                               'pymdownx.details': {},
                               'pymdownx.highlight': {'anchor_linenums': True,
                                                      'line_spans': '__span',
                                                      'pygments_lang_class': True},
                               'pymdownx.superfences': {'custom_fences': [{'class': 'mermaid',
                                                                           'format': <function fence_code_format at 0x7ff77e88b060>,
                                                                           'name': 'mermaid'}]},
                               'pymdownx.tabbed': {},
                               'tables': {}},
          plugins=[Plugin('mkdocstrings')],
          js=[],
          assets=[],
          packages=[])
Metadata
created:
  source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/nodes_section.py
  source_function: create_section_for_nodes
  source_line_no: 93
hide:
- toc
icon: material/timeline
status: js
title: MkTimeline