<?php

function writeFile($path, $content) {
	echo "Write “".$path."”…";
	$f = fopen($path, "w");
	fwrite($f, $content);
	fclose($f);
	echo "Ok\n";
}

function extractNode($node) {
	$alt = $node->getAttribute("alt");
	if (!$alt) return;

	if (substr($alt, -4) == ".png") {
		writeFile(substr($alt, 0, -4).".mml", '<?xml version="1.0" encoding="UTF-8"?>'."\n".$node->C14N());
	}
}

function extractFromFile($path) {
	$dom = new DOMDocument();
	@$dom->loadHTMLFile($path);
	$nodes = $dom->getElementsByTagName("math");
	foreach($nodes as $node) {
		extractNode($node);
	}
}

function extractFromFiles($list) {
	foreach($list as $path) {
		extractFromFile($path);
	}
}


unset($argv[0]);
extractFromFiles($argv);
