<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

package WikiText::ParseHeadings;

# Parses out headings from wikitext

my $structure = [];
my $pointer = { 0 =&gt; $structure };
my $content_buffer;
my $level = 0;

sub parse_line {
	my ($unused, $_) = @_;
	
	my $orig = $_;
	s/\s*$//g;
	
	if (m/^(\=+)\s*(.*?)\s*\=+$/) {
		$level = length($1)-1;
		my $insertLevel = $level - 1;
		
		push @{$pointer-&gt;{$level}}, $content_buffer;
		my $insert = { 'title' =&gt; $2, 'content' =&gt; [] };
		push @{$pointer-&gt;{$insertLevel}}, $insert;
		
		$pointer-&gt;{$level} = $insert-&gt;{'content'};
	} else {
		$content_buffer .= $orig;
	}
}

sub finish_parse {
	push @{$pointer-&gt;{$level}}, $content_buffer;
}

sub reset_state {
	$structure = [];
	$pointer = { 0 =&gt; $structure };
	$content_buffer = '';
	$level = 0;
}

sub structure { return $structure; }

1;
</pre></body></html>