Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
markdown/markdown/blockparser.py at 3.11.2 · Python-Markdown/markdown · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Python-Markdown
markdown
Repository navigation
Code
Issues
13
(13)
Pull requests
7
(7)
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
3.11.2
Breadcrumbs
markdown
/
markdown
/
blockparser.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
160 lines (118 loc) · 5.59 KB
3.11.2
Breadcrumbs
markdown
/
markdown
/
blockparser.py
Copy path
Top
File metadata and controls
Code
Blame
160 lines (118 loc) · 5.59 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Python Markdown
# A Python implementation of John Gruber's Markdown.
# Documentation: https://python-markdown.github.io/
# GitHub: https://github.com/Python-Markdown/markdown/
# PyPI: https://pypi.org/project/Markdown/
# Started by Manfred Stienstra (http://www.dwerg.net/).
# Maintained for a few years by Yuri Takhteyev (http://www.freewisdom.org).
# Currently maintained by Waylan Limberg (https://github.com/waylan),
# Dmitry Shachnev (https://github.com/mitya57) and Isaac Muse (https://github.com/facelessuser).
# Copyright 2007-2023 The Python Markdown Project (v. 1.7 and later)
# Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b)
# Copyright 2004 Manfred Stienstra (the original version)
# License: BSD (see LICENSE.md for details).
"""
The block parser handles basic parsing of Markdown blocks. It doesn't concern
itself with inline elements such as `**bold**` or `*italics*`, but rather just
catches blocks, lists, quotes, etc.
The `BlockParser` is made up of a bunch of `BlockProcessors`, each handling a
different type of block. Extensions may add/replace/remove `BlockProcessors`
as they need to alter how Markdown blocks are parsed.
"""
from
__future__
import
annotations
import
xml
.
etree
.
ElementTree
as
etree
from
typing
import
TYPE_CHECKING
,
Iterable
,
Any
from
.
import
util
if
TYPE_CHECKING
:
# pragma: no cover
from
markdown
import
Markdown
from
.
blockprocessors
import
BlockProcessor
class
State
(
list
):
""" Track the current and nested state of the parser.
This utility class is used to track the state of the `BlockParser` and
support multiple levels if nesting. It's just a simple API wrapped around
a list. Each time a state is set, that state is appended to the end of the
list. Each time a state is reset, that state is removed from the end of
the list.
Therefore, each time a state is set for a nested block, that state must be
reset when we back out of that level of nesting or the state could be
corrupted.
While all the methods of a list object are available, only the three
defined below need be used.
"""
def
set
(
self
,
state
:
Any
):
""" Set a new state. """
self
.
append
(
state
)
def
reset
(
self
)
->
None
:
""" Step back one step in nested state. """
self
.
pop
()
def
isstate
(
self
,
state
:
Any
)
->
bool
:
""" Test that top (current) level is of given state. """
if
len
(
self
):
return
self
[
-
1
]
==
state
else
:
return
False
class
BlockParser
:
""" Parse Markdown blocks into an `ElementTree` object.
A wrapper class that stitches the various `BlockProcessors` together,
looping through them and creating an `ElementTree` object.
"""
def
__init__
(
self
,
md
:
Markdown
):
""" Initialize the block parser.
Arguments:
md: A Markdown instance.
Attributes:
BlockParser.md (Markdown): A Markdown instance.
BlockParser.state (State): Tracks the nesting level of current location in document being parsed.
BlockParser.blockprocessors (util.Registry): A collection of
[`blockprocessors`][markdown.blockprocessors].
"""
self
.
blockprocessors
:
util
.
Registry
[
BlockProcessor
]
=
util
.
Registry
()
self
.
state
=
State
()
self
.
md
=
md
def
parseDocument
(
self
,
lines
:
Iterable
[
str
])
->
etree
.
ElementTree
:
""" Parse a Markdown document into an `ElementTree`.
Given a list of lines, an `ElementTree` object (not just a parent
`Element`) is created and the root element is passed to the parser
as the parent. The `ElementTree` object is returned.
This should only be called on an entire document, not pieces.
Arguments:
lines: A list of lines (strings).
Returns:
An element tree.
"""
# Create an `ElementTree` from the lines
self
.
root
=
etree
.
Element
(
self
.
md
.
doc_tag
)
self
.
parseChunk
(
self
.
root
,
'
\n
'
.
join
(
lines
))
return
etree
.
ElementTree
(
self
.
root
)
def
parseChunk
(
self
,
parent
:
etree
.
Element
,
text
:
str
)
->
None
:
""" Parse a chunk of Markdown text and attach to given `etree` node.
While the `text` argument is generally assumed to contain multiple
blocks which will be split on blank lines, it could contain only one
block. Generally, this method would be called by extensions when
block parsing is required.
The `parent` `etree` Element passed in is altered in place.
Nothing is returned.
Arguments:
parent: The parent element.
text: The text to parse.
"""
self
.
parseBlocks
(
parent
,
text
.
split
(
'
\n
\n
'
))
def
parseBlocks
(
self
,
parent
:
etree
.
Element
,
blocks
:
list
[
str
])
->
None
:
""" Process blocks of Markdown text and attach to given `etree` node.
Given a list of `blocks`, each `blockprocessor` is stepped through
until there are no blocks left. While an extension could potentially
call this method directly, it's generally expected to be used
internally.
This is a public method as an extension may need to add/alter
additional `BlockProcessors` which call this method to recursively
parse a nested block.
Arguments:
parent: The parent element.
blocks: The blocks of text to parse.
"""
while
blocks
:
for
processor
in
self
.
blockprocessors
:
if
processor
.
test
(
parent
,
blocks
[
0
]):
if
processor
.
run
(
parent
,
blocks
)
is
not
False
:
# run returns True or None
break
You can’t perform that action at this time.