summaryrefslogtreecommitdiff
path: root/src/mistune/renderers/_list.py
diff options
context:
space:
mode:
authorArturs Artamonovs <dos21h@gmail.com>2023-01-29 10:30:54 +0000
committerArturs Artamonovs <dos21h@gmail.com>2023-01-29 10:30:54 +0000
commit66fa71a8f11b6ce5e8471b533f67cc3a1fdb85a8 (patch)
tree7aed7f385826a3bd88c76a373e28c6cfae4f396e /src/mistune/renderers/_list.py
parent129c1201ea5c4418f0f89ad932633c7cea2439b7 (diff)
downloadmd-site-66fa71a8f11b6ce5e8471b533f67cc3a1fdb85a8.tar.gz
md-site-66fa71a8f11b6ce5e8471b533f67cc3a1fdb85a8.zip
Update to new mistune, removed old mistune, rewrite to python3
Diffstat (limited to 'src/mistune/renderers/_list.py')
-rw-r--r--src/mistune/renderers/_list.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/mistune/renderers/_list.py b/src/mistune/renderers/_list.py
new file mode 100644
index 0000000..0a18639
--- /dev/null
+++ b/src/mistune/renderers/_list.py
@@ -0,0 +1,60 @@
+from ..util import strip_end
+
+
+def render_list(renderer, token, state) -> str:
+ attrs = token['attrs']
+ if attrs['ordered']:
+ children = _render_ordered_list(renderer, token, state)
+ else:
+ children = _render_unordered_list(renderer, token, state)
+
+ text = ''.join(children)
+ parent = token.get('parent')
+ if parent:
+ if parent['tight']:
+ return text
+ return text + '\n'
+ return strip_end(text) + '\n'
+
+
+def _render_list_item(renderer, parent, item, state):
+ leading = parent['leading']
+ text = ''
+ for tok in item['children']:
+ if tok['type'] == 'list':
+ tok['parent'] = parent
+ elif tok['type'] == 'blank_line':
+ continue
+ text += renderer.render_token(tok, state)
+
+ lines = text.splitlines()
+ text = lines[0] + '\n'
+ prefix = ' ' * len(leading)
+ for line in lines[1:]:
+ if line:
+ text += prefix + line + '\n'
+ else:
+ text += '\n'
+ return leading + text
+
+
+def _render_ordered_list(renderer, token, state):
+ attrs = token['attrs']
+ start = attrs.get('start', 1)
+ for item in token['children']:
+ leading = str(start) + token['bullet'] + ' '
+ parent = {
+ 'leading': leading,
+ 'tight': token['tight'],
+ }
+ yield _render_list_item(renderer, parent, item, state)
+ start += 1
+
+
+def _render_unordered_list(renderer, token, state):
+ parent = {
+ 'leading': token['bullet'] + ' ',
+ 'tight': token['tight'],
+ }
+ for item in token['children']:
+ yield _render_list_item(renderer, parent, item, state)