| 1 |
from docutils import nodes |
| 2 |
from docutils.parsers.rst import directives |
| 3 |
from sphinx.application import Sphinx |
| 4 |
|
| 5 |
from .icons import get_octicon |
| 6 |
from .shared import SEMANTIC_COLORS, SdDirective, create_component, make_choice |
| 7 |
|
| 8 |
|
| 9 |
def setup_article_info(app: Sphinx): |
| 10 |
"""Setup the article information components.""" |
| 11 |
app.add_directive("article-info", ArticleInfoDirective) |
| 12 |
|
| 13 |
|
| 14 |
class ArticleInfoDirective(SdDirective): |
| 15 |
""" """ |
| 16 |
|
| 17 |
has_content = False |
| 18 |
required_arguments = 0 |
| 19 |
optional_arguments = 0 |
| 20 |
option_spec = { |
| 21 |
"avatar": directives.uri, |
| 22 |
"avatar-alt": directives.unchanged, |
| 23 |
"avatar-link": directives.uri, |
| 24 |
"avatar-outline": make_choice(SEMANTIC_COLORS), |
| 25 |
"author": directives.unchanged_required, |
| 26 |
"date": directives.unchanged_required, |
| 27 |
"read-time": directives.unchanged_required, |
| 28 |
"class-container": directives.class_option, |
| 29 |
"class-avatar": directives.class_option, |
| 30 |
} |
| 31 |
|
| 32 |
def _parse_text( |
| 33 |
self, text: str, icon: nodes.Node | None = None, parse: bool = False |
| 34 |
) -> nodes.Node: |
| 35 |
"""Parse the text.""" |
| 36 |
if not parse: |
| 37 |
output = ([icon] if icon else []) + [nodes.Text(text)] |
| 38 |
else: |
| 39 |
text_nodes, _ = self.state.inline_text(text, self.lineno) |
| 40 |
text_nodes = ([icon] if icon else []) + text_nodes |
| 41 |
# note certain nodes (like references) need to be nested in a TextElement node |
| 42 |
# (e.g. a pargraph) |
| 43 |
para = nodes.paragraph("", "", *text_nodes, classes=["sd-p-0", "sd-m-0"]) |
| 44 |
self.set_source_info(para) |
| 45 |
output = [para] |
| 46 |
return output |
| 47 |
|
| 48 |
def run_with_defaults(self) -> list[nodes.Node]: # noqa: PLR0915 |
| 49 |
parse_fields = True # parse field text |
| 50 |
|
| 51 |
top_grid = create_component( |
| 52 |
"grid-container", |
| 53 |
[ |
| 54 |
"sd-container-fluid", |
| 55 |
"sd-sphinx-override", |
| 56 |
"sd-p-0", |
| 57 |
"sd-mt-2", |
| 58 |
"sd-mb-4", |
| 59 |
*self.options.get("class-container", []), |
| 60 |
], |
| 61 |
) |
| 62 |
self.set_source_info(top_grid) |
| 63 |
|
| 64 |
top_row = create_component( |
| 65 |
"grid-row", |
| 66 |
["sd-row", "sd-row-cols-2", "sd-gx-2", "sd-gy-1"], |
| 67 |
) |
| 68 |
self.set_source_info(top_row) |
| 69 |
top_grid += top_row |
| 70 |
|
| 71 |
avatar_uri = self.options.get("avatar") |
| 72 |
if avatar_uri: |
| 73 |
# TODO only in html (hide in latex) |
| 74 |
avatar_column = create_component( |
| 75 |
"grid-item", |
| 76 |
["sd-col", "sd-col-auto", "sd-d-flex-row", "sd-align-minor-center"], |
| 77 |
) |
| 78 |
self.set_source_info(avatar_column) |
| 79 |
avatar_classes = ["sd-avatar-sm"] |
| 80 |
if "avatar-outline" in self.options: |
| 81 |
avatar_classes.append(f"sd-outline-{self.options['avatar-outline']}") |
| 82 |
if "class-avatar" in self.options: |
| 83 |
avatar_classes += self.options["class-avatar"] |
| 84 |
avatar_image = nodes.image( |
| 85 |
"", |
| 86 |
uri=avatar_uri, |
| 87 |
alt=self.options.get("avatar-alt", ""), |
| 88 |
classes=avatar_classes, |
| 89 |
) |
| 90 |
self.set_source_info(avatar_image) |
| 91 |
if self.options.get("avatar-link"): |
| 92 |
avatar_link = nodes.reference( |
| 93 |
"", "", refuri=self.options.get("avatar-link") |
| 94 |
) |
| 95 |
avatar_link += avatar_image |
| 96 |
avatar_image = avatar_link |
| 97 |
avatar_column += avatar_image |
| 98 |
top_row += avatar_column |
| 99 |
|
| 100 |
info_column = create_component( |
| 101 |
"grid-item", |
| 102 |
["sd-col", "sd-d-flex-row", "sd-align-minor-center"], |
| 103 |
) |
| 104 |
self.set_source_info(info_column) |
| 105 |
top_row += info_column |
| 106 |
|
| 107 |
info_grid = create_component( |
| 108 |
"grid-container", |
| 109 |
[ |
| 110 |
"sd-container-fluid", |
| 111 |
"sd-sphinx-override", |
| 112 |
], |
| 113 |
) |
| 114 |
self.set_source_info(info_grid) |
| 115 |
info_column += info_grid |
| 116 |
|
| 117 |
info_row = create_component( |
| 118 |
"grid-row", |
| 119 |
[ |
| 120 |
"sd-row", |
| 121 |
"sd-row-cols-2", |
| 122 |
"sd-row-cols-xs-2", |
| 123 |
"sd-row-cols-sm-3", |
| 124 |
"sd-row-cols-md-3", |
| 125 |
"sd-row-cols-lg-3", |
| 126 |
"sd-gx-3", |
| 127 |
"sd-gy-1", |
| 128 |
], |
| 129 |
) |
| 130 |
self.set_source_info(info_row) |
| 131 |
info_grid += info_row |
| 132 |
|
| 133 |
author_text = self.options.get("author") |
| 134 |
if author_text: |
| 135 |
author_column = create_component( |
| 136 |
"grid-item", |
| 137 |
["sd-col", "sd-col-auto", "sd-d-flex-row", "sd-align-minor-center"], |
| 138 |
) |
| 139 |
self.set_source_info(author_column) |
| 140 |
author_nodes = self._parse_text(author_text, parse=parse_fields) |
| 141 |
author_column.extend(author_nodes) |
| 142 |
info_row += author_column |
| 143 |
|
| 144 |
date_text = self.options.get("date") |
| 145 |
if date_text: |
| 146 |
date_column = create_component( |
| 147 |
"grid-item", |
| 148 |
["sd-col", "sd-col-auto", "sd-d-flex-row", "sd-align-minor-center"], |
| 149 |
) |
| 150 |
self.set_source_info(date_column) |
| 151 |
date_icon = nodes.raw( |
| 152 |
"", |
| 153 |
nodes.Text(get_octicon("calendar", height="16px")), |
| 154 |
classes=["sd-pr-2"], |
| 155 |
format="html", |
| 156 |
) |
| 157 |
date_nodes = self._parse_text(date_text, icon=date_icon, parse=parse_fields) |
| 158 |
date_column.extend(date_nodes) |
| 159 |
info_row += date_column |
| 160 |
|
| 161 |
read_time_text = self.options.get("read-time") |
| 162 |
if read_time_text: |
| 163 |
read_time_column = create_component( |
| 164 |
"grid-item", |
| 165 |
["sd-col", "sd-col-auto", "sd-d-flex-row", "sd-align-minor-center"], |
| 166 |
) |
| 167 |
self.set_source_info(read_time_column) |
| 168 |
read_time_icon = nodes.raw( |
| 169 |
"", |
| 170 |
nodes.Text(get_octicon("clock", height="16px")), |
| 171 |
classes=["sd-pr-2"], |
| 172 |
format="html", |
| 173 |
) |
| 174 |
read_time_nodes = self._parse_text( |
| 175 |
read_time_text, icon=read_time_icon, parse=parse_fields |
| 176 |
) |
| 177 |
read_time_column.extend(read_time_nodes) |
| 178 |
info_row += read_time_column |
| 179 |
|
| 180 |
return [top_grid] |