global !p
# Smartly automate inserting of certain characters. 
# Mainly used for smart space insertion. 
def smart_space(next_str, auto_str=" ", loose=False):
    next_word = ""

    if next_str:
        if loose == True:
            next_word = auto_str
        elif next_str[0] in [",", ".", "-", "!", "?", " "]:
            next_word = auto_str

    return next_word
endglobal

# Text formatting
snippet "h(([1-6]))" "Quick header snippet" bir
`!p
header_level = int(match.group(1))
legit_header_level = True if header_level >= 1 and header_level <= 6 else False

if header_level == 2:
    snip.rv = "\n" * 4
else:
    snip.rv = "\n" * 2

snip.rv += "=" * header_level if legit_header_level else ""
` ${1:Chapter name}

$0
endsnippet

snippet bf "Boldface" iw
**$1** $0
endsnippet

snippet it "Italic" iw
__$1__ $0
endsnippet

snippet tt "Monospace" iw
\`$1\` $0
endsnippet

snippet hl "Highlighted text" iw
#$1# $0
endsnippet

snippet a "Hyperlink" iw
${1:<url>}[${2:<text>}] $0
endsnippet

snippet link "Link for files" iw
link:${1:<url>}[${2:\`$1\`}]
endsnippet

snippet sp "Superscript" iw
^$1^ $0
endsnippet

snippet sb "Subscript" iw
~$1~ $0
endsnippet

snippet dt "Definition term" bi
${1:<term>}::
${2:<definition>}
$0
endsnippet

snippet ul "Unordered list item" bi
* ${1:<list item>}
$0
endsnippet

snippet ol "Ordered list item" bi
. ${1:<list item>}
$0
endsnippet

snippet -ol "Reversed ordered list (use it only once in a list)" bi
[%reversed]
. ${1:<list item>}
endsnippet

snippet bquote "Blockquote" bi
----
$1
----
$0
endsnippet

snippet src "Source code listings" bi
[source`!p snip.rv=smart_space(t[1], ", ", loose=True)`${1:<language>}]
----
${2:<source code>}
----
$0
endsnippet

snippet "-table (\d+)-" "Make a quick table" bir
[cols="`!p snip.rv = match.group(1)`*"]
|===

$1

|===
$0
endsnippet

snippet "-table h (\d+)-" "Make a quick table with the headers already set up" bir
[cols="`!p snip.rv = match.group(1)`", options="headers"`!p snip.rv=smart_space(t[1], ", ", loose=True)`$1]
|===
`!p
number_of_headers = int(match.group(1))

for i in range(0, number_of_headers):
  snip += f"| Header {i + 1}"
`
$2

|===
$0
endsnippet

# Multimedia blocks
snippet figure "Image block with caption" bi
.${2:<caption>}
image::${1:<image path>}[$2, ${3:width=100%,height=100%}]
$0
endsnippet

snippet -figure "Inline image" i
image:${1:<image path>}[${2}] $0
endsnippet

snippet video "Video block" bi
video::${1:<image path>}[$2]
$0
endsnippet

snippet youtube "YouTube video block" bi
video::${1:<YouTube video ID>}[youtube]
$0
endsnippet

snippet vimeo "Vimeo video block" bi
video::${1:<Vimeo video ID>}[vimeo]
$0
endsnippet

snippet audio "Audio block" bi
audio::${1:<image path>}[`!p snip.rv=smart_space(t[2], "options=", loose=True)`$2]
$0
endsnippet

# Other features
snippet incl "Include document" bi
include::${1:<file path>}[${2:<options>}]
$0
endsnippet

snippet oblock "Open block" bi
--
$1
--
$0
endsnippet

snippet verbatim "Literal text block" bi
....
$1
....
$0
endsnippet


snippet sidebar "Sidebar as an open block" bi
[sidebar]
.${1:<title>}
--
$2
--
$0
endsnippet

snippet abstract "Abstract block" bi
[abstract]
== ${1:Summary}
$0
endsnippet

snippet append "Appendix block" bi
`!p snip.rv = "\n" * 4`
[appendix]
== ${1:Additional readings}
$0
endsnippet

snippet bibres "Bibliographical resource in a definition block" bi
${1:<link>}[${2:<title>}] (retrieved ${3:date})::
${4:<description>}
endsnippet


snippet sidebar "Sidebar block" bi
${1:.<optional title>}
****
$2
****
endsnippet

snippet stem "Stem block" bi
[stem]
++++
$1
++++
$0
endsnippet

snippet stem "Stem macro" iw
stem:[$1] $0
endsnippet

snippet foot "Footnote macro" iw
footnote:[$1] $0
endsnippet

snippet qn "Question and answer" iw
[qanda]
${1:QUESTION}::
${2:ANSWER}
endsnippet

