Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
Binary paint shop problem
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jiří Kalvoda
Binary paint shop problem
Commits
baf12a48
Commit
baf12a48
authored
1 year ago
by
Jiří Kalvoda
Browse files
Options
Downloads
Patches
Plain Diff
Zapomenuté soubory
parent
fbfb0f63
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
prace/bakalarka/bakalarka.py
+151
-0
151 additions, 0 deletions
prace/bakalarka/bakalarka.py
prace/bakalarka/formatitko_commands.py
+34
-0
34 additions, 0 deletions
prace/bakalarka/formatitko_commands.py
with
185 additions
and
0 deletions
prace/bakalarka/bakalarka.py
0 → 100644
+
151
−
0
View file @
baf12a48
#!/usr/bin/env python3
import
argparse
import
sys
import
tempfile
import
subprocess
import
shutil
import
io
import
os
,
pathlib
import
plotly
as
plotly_lib
import
plotly.graph_objects
as
go
import
formatitko
from
formatitko.transform
import
transform
from
formatitko.util
import
import_md
from
formatitko.context
import
Context
,
BlockGroup
from
formatitko.images
import
ImageProcessor
from
formatitko.output_generator
import
OutputGenerator
from
formatitko.transform_processor
import
TransformProcessor
from
formatitko.tex_generator
import
UCWTexGenerator
from
panflute
import
convert_text
import
panflute
as
pf
from
.
import
formatitko_commands
as
fc_module
formatitko_commands
=
{
k
:
v
for
k
,
v
in
fc_module
.
formatitko_commands
.
items
()}
def
formatitko_command
(
f
):
name
=
f
.
__name__
if
name
.
startswith
(
"
formatitko_
"
):
name
=
name
[
len
(
"
formatitko_
"
):]
assert
name
not
in
formatitko_commands
formatitko_commands
[
name
]
=
f
return
f
@formatitko_command
def
formatitko_code_and_output
(
element
,
context
):
import
panflute
as
pf
code
=
element
.
text
old_stdout
=
sys
.
stdout
output
=
io
.
StringIO
()
sys
.
stdout
=
output
exec
(
code
)
sys
.
stdout
=
old_stdout
attributes
=
{}
for
name
in
[
"
highlight
"
]:
if
name
in
element
.
classes
:
attributes
[
name
]
=
element
.
classes
[
"
name
"
]
return
[
pf
.
CodeBlock
(
code
,
classes
=
element
.
classes
,
attributes
=
attributes
),
pf
.
Para
(
pf
.
Str
(
"
Output:
"
)),
pf
.
CodeBlock
(
output
.
getvalue
(),
classes
=
[
"
text
"
],
attributes
=
{
"
highlight
"
:
"
True
"
})
]
@formatitko_command
def
formatitko_plotly
(
element
,
context
):
code
=
element
.
text
globals
=
{
"
plotly
"
:
plotly_lib
,
"
go
"
:
go
,
"
fig
"
:
go
.
Figure
()}
exec
(
code
,
globals
)
fig
=
globals
[
"
fig
"
]
file
=
element
.
attributes
[
"
name
"
]
+
"
.pdf
"
plotly_lib
.
io
.
kaleido
.
scope
.
mathjax
=
None
plotly_lib
.
io
.
write_image
(
fig
,
file
,
format
=
'
pdf
'
,
width
=
650
)
return
[
pf
.
Image
(
url
=
file
)]
@formatitko_command
def
formatitko_head
(
element
,
context
):
keywords
=
"
\n
"
.
join
(
f
"
/field
{
i
+
1
}
{{
{
v
}
}}
"
for
i
,
v
in
enumerate
(
context
.
get_metadata
(
"
ft.keywords
"
)
+
context
.
get_metadata
(
"
ft.en.keywords
"
)))
return
[
pf
.
RawBlock
(
f
"""
\\
startmetadata
dc:creator {{
{
context
.
get_metadata
(
"
ft.author
"
)
}
}}
dc:publisher {{Univerzita Karlova}}
dc:subject
{
keywords
}
dc:description {{
{
context
.
get_metadata
(
"
ft.abstract
"
)
}
}}
/{{en_US}} {{
{
context
.
get_metadata
(
"
ft.en.abstract
"
)
}
}}
stopmetadata
%\setmetadata dc:title {{
{
context
.
get_metadata
(
"
ft.title
"
)
}
}}
%\setmetadata /{{en_US}} dc:title {{Nemcotitul}}
\\
def
\\
ftTitle{{
{
context
.
get_metadata
(
"
ft.title
"
)
}
}}
\\
def
\\
ftAuthor{{
{
context
.
get_metadata
(
"
ft.author
"
)
}
}}
\\
def
\\
ftDepartment{{
{
context
.
get_metadata
(
"
ft.department
"
)
}
}}
\\
def
\\
ftDepartmentType{{
{
context
.
get_metadata
(
"
ft.department_type
"
)
or
"
Katedra
"
}
}}
\\
def
\\
ftAbstract{{
{
context
.
get_metadata
(
"
ft.abstract
"
)
}
}}
\\
def
\\
ftKeywords{{
{
"
,
"
.
join
(
context
.
get_metadata
(
"
ft.keywords
"
))
}
}}
\\
def
\\
ftSupervisor{{
{
context
.
get_metadata
(
"
ft.supervisor
"
)
}
}}
\\
def
\\
ftSupervisorDepartment{{
{
context
.
get_metadata
(
"
ft.supervisor_department
"
)
or
context
.
get_metadata
(
"
ft.department
"
)
}
}}
\\
def
\\
ftStudyProgramme{{
{
context
.
get_metadata
(
"
ft.study_programme
"
)
}
}}
\\
def
\\
ftStudyBranch{{
{
context
.
get_metadata
(
"
ft.study_branch
"
)
}
}}
\\
def
\\
ftYear{{
{
context
.
get_metadata
(
"
ft.year
"
)
}
}}
\\
def
\\
ftTitleEN{{
{
context
.
get_metadata
(
"
ft.en.title
"
)
}
}}
\\
def
\\
ftDepartmentEN{{
{
context
.
get_metadata
(
"
ft.en.department
"
)
}
}}
\\
def
\\
ftDepartmentTypeEN{{
{
context
.
get_metadata
(
"
ft.en.department_type
"
)
or
"
Department
"
}
}}
\\
def
\\
ftAbstractEN{{
{
context
.
get_metadata
(
"
ft.en.abstract
"
)
}
}}
\\
def
\\
ftKeywordsEN{{
{
"
,
"
.
join
(
context
.
get_metadata
(
"
ft.en.keywords
"
))
}
}}
\\
def
\\
ftSupervisorDepartmentEN{{
{
context
.
get_metadata
(
"
ft.en.supervisor_department
"
)
or
context
.
get_metadata
(
"
ft.en.department
"
)
}
}}
\\
input head.tex
"""
,
format
=
"
tex
"
),
]
@formatitko_command
def
formatitko_tail
(
element
,
context
):
return
[]
def
main
():
# Use panflute to parse the input MD file
doc
=
import_md
(
open
(
"
bakalarka/index.md
"
,
"
r
"
).
read
())
OutputGenerator
(
sys
.
stdout
).
generate
(
doc
)
tp
=
TransformProcessor
(
"
bakalarka/index.md
"
)
tp
.
add_command_module
(
formatitko_commands
)
doc
=
tp
.
transform
(
doc
)
imageProcessor
=
ImageProcessor
(
"
img_public_dir
"
,
"
img_web_path
"
,
"
img_cache_dir
"
,
"
.
"
)
with
open
(
"
formatitko.tex
"
,
"
w
"
)
as
file
:
d
=
pathlib
.
Path
(
"
/
"
.
join
(
formatitko
.
__file__
.
split
(
"
/
"
)[:
-
1
]))
print
(
formatitko
.
__file__
,
d
)
with
open
(
d
/
"
formatitko.tex
"
)
as
source
:
file
.
write
(
source
.
read
())
exit
(
0
)
with
open
(
"
bakalarka.tex
"
,
"
w
"
)
as
file
:
UCWTexGenerator
(
file
,
imageProcessor
).
generate
(
doc
)
subprocess
.
run
([
"
luatex
"
,
"
bakalarka
"
],
check
=
True
)
with
open
(
"
bakalarka-vera.html
"
,
"
w
"
)
as
file
:
vera_run
=
subprocess
.
run
([
"
verapdf
"
,
"
bakalarka.pdf
"
,
"
-f
"
,
"
2u
"
,
"
--format
"
,
"
html
"
],
stdout
=
file
)
if
vera_run
.
returncode
:
print
(
f
"
VERA PDF-A VALIDATION FAILD (returncode
{
vera_run
.
returncode
}
)
"
)
print
(
f
"
Details: file://
{
os
.
getcwd
()
}
/bakalarka-vera.html
"
)
exit
(
vera_run
.
returncode
)
if
__name__
==
"
__main__
"
:
main
()
def
__main__
():
main
()
This diff is collapsed.
Click to expand it.
prace/bakalarka/formatitko_commands.py
0 → 100644
+
34
−
0
View file @
baf12a48
import
panflute
as
pf
from
formatitko.command_env
import
parse_string
formatitko_commands
=
{}
def
formatitko_command
(
f
):
name
=
f
.
__name__
assert
name
not
in
formatitko_commands
formatitko_commands
[
name
]
=
f
return
f
def
make_emph
(
*
elements
):
out
=
[]
for
e
in
elements
:
if
isinstance
(
e
,
pf
.
Para
)
or
isinstance
(
e
,
pf
.
Plain
):
out
.
append
(
type
(
e
)(
pf
.
Emph
(
*
e
.
content
)))
elif
isinstance
(
e
,
pf
.
BulletList
):
out
.
append
(
type
(
e
)(
*
(
pf
.
ListItem
(
*
make_emph
(
*
i
.
content
))
for
i
in
e
.
content
)))
else
:
raise
NotImplemented
()
return
out
@formatitko_command
def
box
(
element
,
context
):
out
=
[]
for
e
in
element
.
content
:
if
not
out
:
if
isinstance
(
e
,
pf
.
Para
):
out
.
append
(
pf
.
Para
(
pf
.
Strong
(
*
parse_string
(
"
Fakt:
"
)),
pf
.
Emph
(
*
e
.
content
)))
else
:
raise
NotImplemented
()
else
:
out
+=
make_emph
(
e
)
return
out
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