#! /usr/bin/env tclsh
# Copyright (c) 2018, 2020-2021, 2024
# D. Bohdan and contributors listed in AUTHORS. This code is released under
# the terms of the MIT license. See the file LICENSE for details.

package require tcltest 2

if {![namespace exists ::tclssg]} {
    namespace eval ::tclssg {}
    namespace eval ::tclssg::vendor {}

    lappend auto_path [file dirname [file dirname [file normalize $argv0/___]]]
    package require tclssg::vendor::Markdown
}

proc unindent text {
    set chars { }
    set max inf

    regsub ^\n $text {} text
    regsub \n\[$chars\]*?$ $text {} text

    set rLeading ^\[$chars\]*
    set rBlankLine $rLeading$

    foreach line [split $text \n] {
        if {$line eq {} || [regexp $rBlankLine $line]} continue

        regexp -indices $rLeading $line idc
        set count [expr {[lindex $idc 1] + 1}]

        set max [expr {$max > $count ? $count : $max}]
    }

    set start [expr { $max == inf ? {end+1} : $max }]

    set result {}
    foreach line [split $text \n] {
        lappend result [string range $line $start end]
    }

    return [join $result \n]
}


tcltest::test markdown-3.1 {Tabs in Markdown} \
-cleanup {unset md} \
-body {
    set md "```\ntarget:\n\tcommand foo bar\n```"
    list [Markdown::convert $md 0] \
         [Markdown::convert $md 1]
} \
-result [list \
    "<pre><code>target:\n    command foo bar</code></pre>" \
    "<pre><code>target:\n\tcommand foo bar</code></pre>" \
]


tcltest::test markdown-4.1 {Fenced code block language 1} \
-cleanup {unset md} \
-body {
    set md "```make\ntarget:\n\tcommand foo bar\n```"
    Markdown::convert $md 1
} \
-result "<pre><code class=\"language-make\">target:\n\tcommand\
         foo bar</code></pre>"

tcltest::test markdown-4.2 {Fenced code block language 2} \
-body {
    Markdown::convert "```!@#$%^&*()\nhi\n```"
} \
-result "<pre><code class=\"language-!@#$%^&amp;*()\">hi</code></pre>"

tcltest::test markdown-4.3 {Fenced code block language 3} \
-body {
    Markdown::convert "```foo bar baz\nhi\n```"
} \
-result "<pre><code class=\"language-foo\">hi</code></pre>"


tcltest::test markdown-5.1 {Newlines in HTML tag 1} \
-body {
    Markdown::convert <div>Hello</div>
} \
-result <div>Hello</div>\n

tcltest::test markdown-5.2 {Newlines in HTML tag 2} \
-body {
    Markdown::convert <div>\nHello\n</div>
} \
-result <div>\nHello\n</div>\n

# The tests markdown-5.{3,4,5} test for the behavior of John Gruber's
# original Markdown.pl.  An implementation of CommonMark would remove
# the repeated newlines and wrap the "Hello" in 5.4-5.5 in a <p>.
tcltest::test markdown-5.3 {Newlines in HTML tag 3} \
-body {
    Markdown::convert <div>\nHello\n\n\n</div>
} \
-result <div>\nHello\n\n\n</div>\n

tcltest::test markdown-5.4 {Newlines in HTML tag 4} \
-body {
    Markdown::convert <div>\n\nHello</div>
} \
-result <div>\n\nHello</div>\n

tcltest::test markdown-5.5 {Newlines in HTML tag 5} \
-body {
    Markdown::convert <div>\n\nHello\n\n\n</div>
} \
-result <div>\n\nHello\n\n\n</div>\n


tcltest::test markdown-6.1 {<hr>} \
-body {
    Markdown::convert hello\n\n---\n\nworld
} \
-result "<p>hello</p>\n\n<hr />\n\n<p>world</p>"

tcltest::test markdown-6.2 {<hr> and HTML} \
-body {
    Markdown::convert <table></table>\n\n---\n\n<table></table>
} \
-result "<table></table>\n\n<hr />\n\n<table></table>\n"


tcltest::test markdown-7.1 {Table} \
-body {
    Markdown::convert [unindent {
        | Foo | Bar | Baz |
        |-----|-----|-----|
        |   1 |   2 |   3 |
        | 4   |  5  | 6    |
    }]
} \
-result [unindent {
    <table class="table">
    <thead>
      <tr>
        <th>Foo</th>
        <th>Bar</th>
        <th>Baz</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
      </tr>
      <tr>
        <td>4</td>
        <td>5</td>
        <td>6</td>
      </tr>
    </tbody>
    </table>
}]\n

tcltest::test markdown-7.2 {Table with HTML} \
-body {
    Markdown::convert [unindent {
        | File name | Description |
        |-|-|
        | <a href="download/x.zip">x.zip</a> | Source code. |
    }]
} \
-result [unindent {
    <table class="table">
    <thead>
      <tr>
        <th>File name</th>
        <th>Description</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><a href="download/x.zip">x.zip</a></td>
        <td>Source code.</td>
      </tr>
    </tbody>
    </table>
}]\n

tcltest::test markdown-7.3 {Table with a single column} \
-body {
    Markdown::convert [unindent {
        | Monocolumn |
        |------------|
        | Yes.       |

        | <a name="foo">Hook</a> |
        |-|
        | <em>Line</em> |
    }]
} \
-result [unindent {
    <table class="table">
    <thead>
      <tr>
        <th>Monocolumn</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Yes.</td>
      </tr>
    </tbody>
    </table>


    <table class="table">
    <thead>
      <tr>
        <th><a name="foo">Hook</a></th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td><em>Line</em></td>
      </tr>
    </tbody>
    </table>
}]\n

tcltest::test markdown-7.4 {Table with only a header} \
-body {
    # This works on GitHub.
    Markdown::convert "|Hello|\n|-----|"
} \
-result [unindent {
    <table class="table">
    <thead>
      <tr>
        <th>Hello</th>
      </tr>
    </thead>
    </table>
}]\n

tcltest::test markdown-7.5 {HTML before a table} \
-body {
    Markdown::convert [unindent {
        <script>
            var x = 5;
            var y = 7;
        </script>

        | Hello! |
        |---|
    }]
} \
-result [unindent {
    <script>
        var x = 5;
        var y = 7;
    </script>

    <table class="table">
    <thead>
      <tr>
        <th>Hello!</th>
      </tr>
    </thead>
    </table>
}]\n


tcltest::test markdown-8.1 {HTML blocks} \
-body {
    Markdown::convert "<div>\n</div>\n\n# Hello!"
} \
-result <div>\n</div>\n\n<h1>Hello!</h1>

tcltest::test markdown-8.2.1 {Newlines after a one-line HTML block} \
-body {
    Markdown::convert "<div></div>\n# Hello!"
} \
-result <div></div>\n\n<h1>Hello!</h1>

tcltest::test markdown-8.2.2 {Newlines after a one-line HTML block} \
-body {
    Markdown::convert "<div></div>\n\n# Hello!"
} \
-result <div></div>\n\n<h1>Hello!</h1>

tcltest::test markdown-8.2.3 {Newlines after a one-line HTML block} \
-body {
    Markdown::convert "<div></div>\n\n\n# Hello!"
} \
-result <div></div>\n\n<h1>Hello!</h1>

tcltest::test markdown-8.3.1 {Newlines after a multiline HTML block} \
-body {
    Markdown::convert "<div>\n</div>\n# Hello!"
} \
-result <div>\n</div>\n\n<h1>Hello!</h1>

tcltest::test markdown-8.3.2 {Newlines after a multiline HTML block} \
-body {
    Markdown::convert "<div>\n</div>\n\n# Hello!"
} \
-result <div>\n</div>\n\n<h1>Hello!</h1>

tcltest::test markdown-8.3.3 {Newlines after a multiline HTML block} \
-body {
    Markdown::convert "<div>\n</div>\n\n\n# Hello!"
} \
-result <div>\n</div>\n\n<h1>Hello!</h1>

tcltest::test markdown-8.4 {Text after the opening/closing tag} \
-body {
    Markdown::convert "<div>   <b>\n</b> </div> <!-- -->\n\n# Hello!"
} \
-result [unindent {
    <div>   <b>
    </b> </div> <!-- -->

    <h1>Hello!</h1>
}]

tcltest::test markdown-8.5 {} \
-body {
    Markdown::convert [unindent {
        <p><img src="https://example.com"></p>

        `broken` markup
    }]
} \
-result [unindent {
    <p><img src="https://example.com"></p>

    <p><code>broken</code> markup</p>
}]


tcltest::test markdown-9.1 {Indented code block} \
-body {
    Markdown::convert "    foo\n    bar\n    baz"
} \
-result [unindent {
    <pre><code>foo
    bar
    baz
    </code></pre>
}]

tcltest::test markdown-9.2 {Indented code block} \
-body {
    Markdown::convert "    foo\n    bar\n    baz\n"
} \
-result [unindent {
    <pre><code>foo
    bar
    baz
    </code></pre>
}]


tcltest::cleanupTests

rename unindent {}
