# encoding: utf-8

module Heidenblog

    class MyPandoc < Nanoc::Filter

        requires 'pandoc-ruby'

        identifier :pandoc2html5
        type :text

        # Runs the content through [Pandoc](http://johnmacfarlane.net/pandoc/)
        # using [PandocRuby](https://github.com/alphabetum/pandoc-ruby). Options
        # are passed on to PandocRuby.
        #
        # @param [String] content The content to filter
        #
        # @return [String] The filtered content
        def run(content, params = {})
            # PandocRuby.convert(content, params)
            # `echo '#{content}' | pandoc --from=markdown --to=html5 --no-wrap --table-of-contents --mathjax --standalone --section-divs`
            # PandocRuby.convert(content,
            #              { :from => :markdown, :to => :html5 }, :no_wrap,
            #              :table_of_contents, :mathjax, :standalone, :section_divs)
            PandocRuby.convert(content, {
                :from => :markdown,
                :to => :html5
                 },
                 :smart,
                 :no_wrap,
                #  :table_of_contents,
                 :mathjax,
                 :standalone,
                 :section_divs
             )
        end

    end

    # Credits to https://gist.github.com/xiaohanyu/9866531
    class PandocHtml < Nanoc::Filter
        identifier :pandoc_html5
        type :text => :text

        def run(content, params = {})
            input_format = case item[:extension]
            when 'org'
                'org'
            when 'md', 'markdown'
                'markdown'
            end

            `pandoc --mathjax -f #{input_format} -t html5 --smart --wrap=none --table-of-contents --section-divs < #{item.raw_filename}`
        end

    end

    # Credits to https://gist.github.com/xiaohanyu/9866531
    class PandocPdf < Nanoc::Filter
        identifier :pandoc_pdf
        type :text => :binary

        require 'tempfile'

        def run(content, params = {})
            temp_pdf_file = Tempfile.new(['nanoc_temp_pdf_file', '.pdf'], '/tmp')

            input_format = case item[:extension]
            when 'org'
                'org'
            when 'md', 'markdown'
                'markdown'
            end

            `pandoc --mathjax -f #{input_format} -t latex --latex-engine xelatex -V geometry:margin=1in < #{item.raw_filename} -o #{temp_pdf_file.path}`

            FileUtils.mv(temp_pdf_file, output_filename)
        end

    end

end
