<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl -sw

use JSON;
use YAML;
use WikiText::ParseHeadings;
use WikiText::ParseDiscussion;

WikiText::ParseHeadings-&gt;reset_state();

while (&lt;&gt;) {
	WikiText::ParseHeadings-&gt;parse_line($_);
}

WikiText::ParseHeadings-&gt;finish_parse();

my $topLevelStructure = WikiText::ParseHeadings-&gt;structure;

recursiveParseStructure( $topLevelStructure );

sub recursiveParseStructure {
	my ($structure, $parent) = @_;
	
	if (ref $structure eq 'HASH') {
		#print "Processing section ".$structure-&gt;{'title'}."\n";
		recursiveParseStructure( $structure-&gt;{'content'}, $structure );
	} elsif (ref $structure eq 'ARRAY') {
		foreach my $subitem (@$structure) {
			recursiveParseStructure($subitem, $structure);
		}
	} else {
		WikiText::ParseDiscussion-&gt;reset_state();
		
		my @lines = split /[\r\n]+/, $structure;
		
		foreach my $line (@lines) {
			WikiText::ParseDiscussion-&gt;input_line($line);
		}
		
		@$parent = WikiText::ParseDiscussion-&gt;get_posts;
	}
}

print encode_json( $topLevelStructure );
</pre></body></html>