#!/usr/bin/python3
import sys

from mutil.XmlExtractor import XmlExtractor
from mutil import XmlExtractor as XMLE

if len(sys.argv) != 2:
	raise RuntimeError("usage: extract_links FILE")

fname = sys.argv[1]
fbase = fname[0:len(fname)-4]
xmlex = XmlExtractor()
xmlex.openInput(fname)
with open("links.txt", "w") as f:
	(ctl,elem, value, attrs) = xmlex.extractElement(XMLE.EC_BEG) # internet
	(ctl,elem, value, attrs) = xmlex.extractElement(XMLE.EC_BEG)
	while ctl != XMLE.EC_END:
		if elem == "div":
			target = attrs["id"]
			(ctl,elem, value, attrs) = xmlex.extractElement(XMLE.EC_BEG) # h1
			while ctl != XMLE.EC_END:
				if elem == "h1":
					f.write(f'<a href="{fbase}.html#{target}" onclick="w3_close()" class="w3-bar-item w3-button w3-hover-white">{value}</a>\n')
				(ctl,elem, value, attrs) = xmlex.extractElement(XMLE.EC_CTN)
		(ctl,elem, value, attrs) = xmlex.extractElement(XMLE.EC_CTN) # div

