Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
Scripts
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Specification Tools
Scripts
Commits
ea4853f4
Commit
ea4853f4
authored
3 months ago
by
Andreas Kraft
Browse files
Options
Downloads
Patches
Plain Diff
Improved support for stringify documents. Added support for stdin/stdout file processing
parent
4f4e21fe
No related branches found
No related tags found
1 merge request
!1
Restructuring and cleaning scripts for Mkdocs
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
toMkdocs/markdownTools.py
+44
-7
44 additions, 7 deletions
toMkdocs/markdownTools.py
with
44 additions
and
7 deletions
toMkdocs/markdownTools.py
+
44
−
7
View file @
ea4853f4
...
...
@@ -9,7 +9,7 @@
"""
Various tools for markdown processing
"""
from
__future__
import
annotations
from
typing
import
Callable
from
typing
import
Callable
,
Optional
from
dataclasses
import
dataclass
import
base64
,
hashlib
...
...
@@ -69,6 +69,7 @@ class LineType(Enum):
TABLESEPARATOR
=
auto
()
TABLEROW
=
auto
()
TABLELASTROW
=
auto
()
RAWHTML
=
auto
()
@dataclass
...
...
@@ -78,6 +79,16 @@ class Line:
lineType
:
LineType
=
LineType
.
TEXT
def
__str__
(
self
)
->
str
:
"""
Return the line as a string.
"""
return
self
.
text
def
__repr__
(
self
)
->
str
:
"""
Return the line as a string.
"""
return
self
.
__str__
()
@dataclass
class
Clause
:
"""
Represents a clause in the markdown file.
"""
...
...
@@ -176,6 +187,18 @@ class Clause:
The number of characters in the clause.
"""
return
sum
([
len
(
l
.
text
.
strip
())
for
l
in
self
.
lines
])
def
__str__
(
self
)
->
str
:
"""
Return the clause as a string.
"""
return
''
.
join
([
str
(
l
)
for
l
in
self
.
lines
])
def
__repr__
(
self
)
->
str
:
"""
Return the clause as a string.
"""
return
self
.
__str__
()
class
Footnote
:
"""
Represents a footnote in the markdown file.
"""
...
...
@@ -361,12 +384,23 @@ class Document:
clause
.
lines
=
lines
def
__str__
(
self
)
->
str
:
"""
Return the document as a string.
"""
return
'
\n
'
.
join
([
str
(
c
)
for
c
in
self
.
clauses
])
def
__repr__
(
self
)
->
str
:
"""
Return the document as a string.
"""
return
self
.
__str__
()
def
analyseMarkdown
(
filename
:
str
)
->
Document
:
def
analyseMarkdown
(
filename
:
Optional
[
str
]
=
None
,
inLines
:
Optional
[
list
[
str
]]
=
None
)
->
Document
:
"""
Analyse the markdown file and split it into clauses.
Either the filename or the inLines must be provided.
Args:
filename: The name of the markdown file.
inLines: The lines of the markdown file.
Returns:
The document object.
...
...
@@ -388,9 +422,7 @@ def analyseMarkdown(filename:str) -> Document:
printDebug
(
htmltable
)
except
Exception
as
e
:
printError
(
f
"
Error:
{
e
}
"
)
# TODO move this outside of the analyseMarkdown function !!!
for
row
in
htmltable
:
outClauses
[
-
1
].
append
(
Line
(
row
,
LineType
.
TABLEROW
))
outClauses
[
-
1
].
append
(
Line
(
htmltable
,
LineType
.
RAWHTML
))
gridTable
=
''
...
...
@@ -398,8 +430,13 @@ def analyseMarkdown(filename:str) -> Document:
# Read the file.
# Note: We use utf-8 and replace errors to avoid problems with special or unknown characters.
with
open
(
filename
,
'
r
'
,
encoding
=
'
utf-8
'
,
errors
=
'
replace
'
)
as
file
:
inLines
=
file
.
readlines
()
if
filename
and
not
inLines
:
with
open
(
filename
,
'
r
'
,
encoding
=
'
utf-8
'
,
errors
=
'
replace
'
)
as
file
:
inLines
=
file
.
readlines
()
elif
not
filename
and
inLines
:
pass
else
:
raise
ValueError
(
'
Either the filename or the lines must be provided.
'
)
# The list of clauses. The first clause contains the text before the first heading.
outClauses
:
list
[
Clause
]
=
[
Clause
(
0
,
''
,
''
,
[])]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment