@ -6,12 +6,28 @@ from transform import FQuoted
# Heavily inspired by: git://git.ucw.cz/labsconf2022.git
# Heavily inspired by: git://git.ucw.cz/labsconf2022.git
def tex ( e , indent_level : int = 0 , indent_str : str = " \t " ) - > str :
def tex ( e , indent_level : int = 0 , indent_str : str = " \t " ) - > str :
content_foot = " "
if hasattr ( e , " attributes " ) and " only " in e . attributes and e . attributes [ " only " ] ! = " tex ":
content_head = " "
return " "
if isinstance ( e , ListContainer ) :
if isinstance ( e , ListContainer ) :
return ' ' . join ( [ tex ( child , indent_level , indent_str ) for child in e ] )
return ' ' . join ( [ tex ( child , indent_level , indent_str ) for child in e ] )
content_foot = " "
content_head = " "
arguments = " "
open = " { "
close = " } "
tag = e . tag . lower ( )
tags = {
Header : " h " + chr ( 64 + e . level ) if hasattr ( e , " level " ) else " " ,
}
if type ( e ) in tags :
tag = tags [ type ( e ) ]
not_implemented = {
not_implemented = {
Citation : True ,
Citation : True ,
Cite : True ,
Cite : True ,
@ -28,7 +44,7 @@ def tex(e, indent_level: int=0, indent_str: str="\t") -> str:
Null : " " ,
Null : " " ,
LineBreak : f " \\ \\ " ,
LineBreak : f " \\ \\ " ,
SoftBreak : f " " ,
SoftBreak : f " " ,
HorizontalRule : " % TODO: hrule "
HorizontalRule : " \\ hr \n \n "
}
}
if type ( e ) in simple_string :
if type ( e ) in simple_string :
return simple_string [ type ( e ) ]
return simple_string [ type ( e ) ]
@ -39,12 +55,6 @@ def tex(e, indent_level: int=0, indent_str: str="\t") -> str:
if isinstance ( e , Para ) :
if isinstance ( e , Para ) :
return tex ( e . content , 0 , " " ) + " \n \n "
return tex ( e . content , 0 , " " ) + " \n \n "
if isinstance ( e , Emph ) :
return f ' {{ \\ it { tex ( e . content , 0 , " " ) } }} '
if isinstance ( e , Strong ) :
return f ' {{ \\ bf { tex ( e . content , 0 , " " ) } }} '
if isinstance ( e , FQuoted ) :
if isinstance ( e , FQuoted ) :
if e . style == " cs " :
if e . style == " cs " :
if e . quote_type == " SingleQuote " :
if e . quote_type == " SingleQuote " :
@ -64,12 +74,53 @@ def tex(e, indent_level: int=0, indent_str: str="\t") -> str:
else :
else :
return f ' " { tex ( e . content , 0 , " " ) } " '
return f ' " { tex ( e . content , 0 , " " ) } " '
if isinstance ( e , BulletList ) :
tag = " list "
open = " "
arguments = " {o} "
close = " \\ endlist "
if isinstance ( e , OrderedList ) :
tag = " list "
open = " "
styles = {
" DefaultStyle " : " n " ,
" Decimal " : " n " ,
" LowerRoman " : " i " ,
" UpperRoman: " : " I " ,
" LowerAlpha " : " a " ,
" UpperAlpha " : " A "
}
style = styles [ e . style ]
delimiters = {
" DefaultDelim " : f " { style } . " ,
" Period " : f " { style } . " ,
" OneParen " : f " { style } ) " ,
" TwoParens " : f " ( { style } ) "
}
style = delimiters [ e . delimiter ]
arguments = f " {{ { style } }} "
close = " \\ endlist "
# FIXME: Starting number of list
if isinstance ( e , ListItem ) :
tag = " : "
if isinstance ( e , Link ) :
tag = " linkurl "
arguments = f ' {{ { e . url } }} '
if isinstance ( e , Math ) :
if isinstance ( e , Math ) :
if e . format == " DisplayMath " :
if e . format == " DisplayMath " :
return f ' $$ { e . text } $$ \n '
return f ' $$ { e . text } $$ \n '
else :
else :
return f ' $ { e . text } $ '
return f ' $ { e . text } $ '
if isinstance ( e , Note ) :
tag = " fn "
if len ( e . content ) == 1 and isinstance ( e . content [ 0 ] , Para ) :
return f ' \\ fn {{ { tex ( e . content [ 0 ] . content , 0 , " " ) } }} '
if isinstance ( e , RawInline ) :
if isinstance ( e , RawInline ) :
if e . format == " tex " :
if e . format == " tex " :
return e . text
return e . text
@ -85,6 +136,12 @@ def tex(e, indent_level: int=0, indent_str: str="\t") -> str:
if isinstance ( e , Span ) or isinstance ( e , Plain ) :
if isinstance ( e , Span ) or isinstance ( e , Plain ) :
return tex ( e . content , 0 , " " )
return tex ( e . content , 0 , " " )
if isinstance ( e , LineItem ) :
return tex ( e . content , 0 , " " ) + ( " \\ \\ \n " if e . next else " \n " )
if isinstance ( e , LineBlock ) :
return f ' { tex ( e . content , indent_level + 1 , indent_str ) } \n '
if isinstance ( e , Div ) :
if isinstance ( e , Div ) :
return f ' { tex ( e . content , indent_level + 1 , indent_str ) } '
return f ' { tex ( e . content , indent_level + 1 , indent_str ) } '
@ -92,15 +149,15 @@ def tex(e, indent_level: int=0, indent_str: str="\t") -> str:
return tex ( e . content , indent_level , indent_str ) + " \n \\ bye "
return tex ( e . content , indent_level , indent_str ) + " \n \\ bye "
if isinstance ( e , Inline ) :
if isinstance ( e , Inline ) :
return f " ( { e . tag } ) { content_head } { tex ( e . content , 0 , ' ' ) } { content_foot } (/ { e . tag } ) "
return f ' \\ { tag } { arguments } { open } { content_head } { tex ( e . content , 0 , " " ) if hasattr ( e , " _content " ) else " " } { e . text if hasattr ( e , " text " ) else " " } { content_foot } { close } '
out_str = " "
out_str = " "
out_str = f " ( { e . tag } ) {{ "
out_str = f " \\ { tag } { arguments } { open } \n "
out_str + = content_head
out_str + = content_head
if hasattr ( e , " _content " ) :
if hasattr ( e , " _content " ) :
out_str + = tex ( e . content , indent_level + 1 , indent_str )
out_str + = tex ( e . content , indent_level + 1 , indent_str )
if hasattr ( e , " text " ) :
if hasattr ( e , " text " ) :
out_str + = e . text
out_str + = e . text
out_str + = f " { content_foot } } }\n \n "
out_str + = f " { content_foot } \n { close } \n \n "
return out_str
return out_str