#|default_exp qmd
Basic qmd generation helpers (experimental)
#|export
from __future__ import annotations
import sys,os,inspect
from fastcore.utils import *
from fastcore.meta import delegates
#|hide
from fastcore.test import *
#| export
def setup():
mod = inspect.getmodule(inspect.currentframe().f_back)
path = Path(mod.__file__)
os.chdir(path.parent)
sys.stdout = open(path.with_suffix('.qmd'), 'w')
print(mod.__doc__)
#| export
def meta(md, # Markdown to add meta to
classes=None, # List of CSS classes to add
style=None, # Dict of CSS styles to add
**kwargs): # Additional attributes to add to meta
"A metadata section for qmd div in `{}`"
if style: kwargs['style'] = "; ".join(f'{k}: {v}' for k,v in style.items())
props = ' '.join(f'{k}="{v}"' for k,v in kwargs.items())
classes = ' '.join('.'+c for c in L(classes))
meta = []
if classes: meta.append(classes)
if props: meta.append(props)
meta = ' '.join(meta)
return md + ("{" + meta + "}" if meta else "")
#| export
def div(txt, # Markdown to add meta to
classes=None, # List of CSS classes to add
style=None, # Dict of CSS styles to add
**kwargs):
"A qmd div with optional metadata section"
return meta("::: ", classes=classes, style=style, **kwargs) + f"\n\n{txt}\n\n:::\n\n"
#| export
def img(fname, # Image to link to
classes=None, # List of CSS classes to add
style=None, # Dict of CSS styles to add
height=None, # Height attribute
relative=None, # Tuple of (position,px)
link=False, # Hyperlink to this image
**kwargs):
"A qmd image"
kwargs,style = kwargs or {}, style or {}
if height: kwargs["height"]= f"{height}px"
if relative:
pos,px = relative
style["position"] = "relative"
style[pos] = f"{px}px"
res = meta(f'![]({fname})', classes=classes, style=style, **kwargs)
return f'[{res}]({fname})' if link else res
#| export
def btn(txt, # Button text
link, # Button link URL
classes=None, # List of CSS classes to add
style=None, # Dict of CSS styles to add
**kwargs):
"A qmd button"
return meta(f'[{txt}]({link})', classes=classes, style=style, role="button")
#|hide
import nbdev; nbdev.nbdev_export()