factory = require "spec.factory"

import trim from require "sitegen.common"

flatten_html = (html) ->
  trim (html\gsub "%>%s+%<", "><")

describe "renderers", ->
  render_for_site = (site, renderer, str, meta={}, page) ->
    page or= factory.Page(:site)
    page.render_fn, page.meta = renderer\load str
    page.meta.template = false
    for k,v in pairs meta
      page.meta[k] = v
    page\render!

  describe "renderers.markdown", ->
    local site, renderer

    render = (...) -> render_for_site site, renderer, ...

    before_each ->
      MarkdownRenderer = require "renderers.markdown"
      site = factory.Site!
      renderer = MarkdownRenderer site

    it "syntax highlights some code", ->
      out = flatten_html render [[
```lua
print("hello world")
```]]

      assert.same [[<div class="highlight"><pre><span></span><code><span class="py-nb">print</span><span class="py-p">(</span><span class="py-s2">&quot;hello world&quot;</span><span class="py-p">)</span></code></pre></div>]], out

    it "syntax highlights moonscript code", ->
      out = flatten_html render [[
```moonscript
print "Hi, #{@name}!"
```]]

      assert.same [[<div class="highlight"><pre><span></span><code><span class="py-nb">print</span><span class="py-s2">&quot;</span><span class="py-s">Hi, </span><span class="py-si">#{</span><span class="py-vc">@name</span><span class="py-si">}</span><span class="py-s">!</span><span class="py-s2">&quot;</span></code></pre></div>]], out

    it "doesnt highlight code inside of a cosmo template", ->
      site.user_vars.hello = (_, args) ->
        { template } = args
        template

      out = flatten_html render [=[
```lua
print 5
```
$hello{[[
  ```lua
  print thing
  ```
]]}]=]

      assert.same [=[<div class="highlight"><pre><span></span><code><span class="py-nb">print</span><span class="py-mi">5</span></code></pre></div><p>
  ```lua
  print thing
  ```
</p>]=], out

    it "doesn't highlight unrecognized code languages", ->
      out = flatten_html render [[
```snickerdoodle
this code block has an unknown label
```]]

      assert.same [[<pre class="snickerdoodle"><code>this code block has an unknown label</code></pre>]], out

    it "doesn't highlight unlabeled code blocks", ->
      out = flatten_html render [[
```
this code block has no label
```]]

      assert.same [[<div class="highlight"><pre><span></span><code>this code block has no label
</code></pre></div>]], out

    it "defaults to pandoc highlighting when pygments fails to recognize code language", ->
      out = flatten_html render [[
```heex
<.greet name="Jane" />
```]]

      assert.same [[<div class="sourceCode" id="cb1"><pre
class="sourceCode heex"><code class="sourceCode elixir"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="op">&lt;.</span>greet name<span class="op">=</span><span class="st">&quot;Jane&quot;</span><span class="op">/&gt;</span></span></code></pre></div>]], out

    it "escapes and unescapes double dollar signs", ->
      out = flatten_html render [[
```Makefile
  $$name
```]]

      assert.same [[<div class="highlight"><pre><span></span><code><span class="py-w"></span><span class="py-nv">$$name</span></code></pre></div>]], out
