#!/usr/bin/env python3
# description: show patterns in markdown
#
#
#
import re, json, os, sys
fn = sys.argv[1]
f = open(fn, "r", encoding="utf-8")
fmt = json.loads(f.read())
# doc urls
search = {
"strftime": "[strftime(3)](https://www.man7.org/linux/man-pages/man3/strftime.3.html#:~:text={})",
"grok": "[grok formats](https://duckduckgo.com/?q=grok+format+{})",
"apache generic": "[mod_log_config.c/log_io.c](https://github.com/apache/httpd/search?q={})",
"else": "[???](https://duckduckgo.com/?q={})",
}
url = search.get(fmt["class"]) or search["else"]
# table head
print(f"\n\n## {fmt['class']}\n")
print("| placeholder \t | id \t | regex \t \t | grok/fmt-recursion \t | description/reference \t |")
print("-------------------------------------------------------------------------------------------")
# fields
for name, opt in fmt.get("fields", {}).items():
id = opt.get("id")
rx = opt.get('rx')
if rx:
rx = rx.replace("|", "¦")
grok = opt.get('grok') or "-"
desc = opt.get('desc') or url.format(id)
print(f"| {name} \t | {id} \t | `{rx}` \t | {grok} \t | {desc} |")
for name, opt in fmt.get("expand", {}).items():
id = opt.get("id")
rx = opt.get('rx') or opt.get("record")
if rx and len(rx) >= 50:
rx = rx[0:50] + "…"
if rx:
rx = rx.replace("|", "¦")
grok = opt.get('grok') or opt.get("class")
desc = opt.get('desc') or url.format(id)
print(f"| {name} \t | {id} \t | `{rx}` \t | {grok} \t | {desc} |")