from os import environ
from os.path import dirname
from pprint import pprint
import re
from sys import stderr
from textwrap import dedent

import pandocfilters as pf


BASE = environ['YEONGHOEY_FILTER_BASE']
DEBUG = environ.get('YEONGHOEY_FILTER_DEBUG') is not None
SRC = environ['YEONGHOEY_FILTER_SRC']

# =============================================================================
# Common utils for filtering
# =============================================================================
CONTEXT_PATH = re.sub(r'^content/', '', dirname(SRC))


def basepath(url):
    if BASE:
        return '/'.join([BASE, CONTEXT_PATH, url])
    else:
        return url


# =============================================================================
# Handle functions
# =============================================================================
# When `handle_<function>` returns:
# pf.Foo : replace (spans as spans, divs as divs only)
#   None : do nohting
#     [] : delete
# =============================================================================


# =============================================================================
# Print handler parameters
# FIXME: Is there any clear way?
# =============================================================================
def handle_debug(*args):
    print(file=stderr)
    names = 'key value format meta'.split()
    for n, a in zip(names, args):
        print(f'{n:6}: ', end='', file=stderr)
        pprint(a, indent=4, stream=stderr)
    print(file=stderr)


# =============================================================================
# Fix basepath for images
# FIXME: process only relative link images
# =============================================================================
def handle_image(key, value, format, meta):
    if key == 'Image':
        attr, inlines, target = value
        url, title = target
        return pf.Image(attr, inlines, (basepath(url), title))


# =============================================================================
# Remove org-tags like :TOC_2_gh:
# =============================================================================
def handle_notag(key, value, format, meta):
    if key == 'Span':
        attr, inlines = value
        _, classes, _ = attr
        if 'tag' in classes:
            return []


# =============================================================================
# Embed pdf files, customized syntax is following:
# : YEONGHOEY_PDF[<width>x<height>] <src>
# =============================================================================
YEONGHOEY_PDF = re.compile(r'''^YEONGHOEY_PDF
                               (?:\[(?P<width>[0-9%]+)x(?P<height>[0-9%]+)\])?
                               [ ]
                               (?P<src>.+)$''', re.VERBOSE)


def handle_pdf(key, value, format, meta):
    assert format == 'html'
    if key == 'CodeBlock':
        attr, code = value

        match = YEONGHOEY_PDF.match(code)
        if match is not None:
            d = match.groupdict(default='100%')
            width = d['width']
            height = d['height']
            src = basepath(d['src'])
            return pf.RawBlock(format, dedent(f'''\
            <iframe src="/ViewerJS/#{src}"
                    type="application/pdf"
                    width="{width}"
                    height="{height}">
              <a href="{src}">{src}</a>
            </iframe>
            '''))


# =============================================================================


if __name__ == '__main__':
    pf.toJSONFilters(filter(None, [
        handle_debug if DEBUG else None,
        handle_image,
        handle_notag,
        handle_pdf,
    ]))

@@ -2658,17 +2658,16 @@
 dth%3E%5B0-9
-%25
 %5D+)x(?P%3C
@@ -2677,17 +2677,16 @@
 ght%3E%5B0-9
-%25
 %5D+)%5C%5D)?%0A
@@ -3015,22 +3015,8 @@
 ict(
-default='100%25'
 )%0A  
@@ -3276,22 +3276,56 @@
     
+style=%22
 width
-=%22
+: 
 %7Bwidth%7D
+vmin; height: %7Bheight%7Dvmin;
 %22%0A  
@@ -3346,25 +3346,23 @@
     
-height=%22%7Bheight%7D%22
+allowfullscreen
 %3E%0A  

Use vim for pdf size