From 734dcade6b8df278c95a9c6dfa5e54f076ee04a0 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sun, 12 Aug 2012 08:34:23 -0400 Subject: Mostly rewrite output(). Heading parsing is done in the format-agnostic parse() method. The only thing left to do to the HTML headings is to set their "id" attributes. --- (limited to 'scripts') diff --git a/scripts/MarkdownBook/Document/HTML.pm b/scripts/MarkdownBook/Document/HTML.pm index 0a2ba90..d250b3b 100644 --- a/scripts/MarkdownBook/Document/HTML.pm +++ b/scripts/MarkdownBook/Document/HTML.pm @@ -17,26 +17,54 @@ use strict; use warnings; use MarkdownBook::Document; -use MarkdownBook::Section::HTML; +use Text::Markdown; use HTML::Template; package MarkdownBook::Document::HTML; our @ISA = qw(MarkdownBook::Document); -sub parse +sub output { my ($self) = @_; - my $file = $self->{'book'}->dir() . '/' . $self->{'file'} . '.html.in'; + my $text; + my $doc; + my $sec; + + $text = $self->{'book'}->subst_macros( + $self->{'source_text'}); + + $text .= "\n\n"; + foreach $doc (@{$self->{'book'}->documents()}) { + foreach $sec (@{$doc->sections()}) { + $text .= '['; + $text .= $sec->id(); + $text .= ']: '; + $text .= $doc->file(); + $text .= '.html#'; + $text .= $sec->id(); + } + } + + $text = Text::Markdown::Markdown($text); + + $self->_do_set_heading_id_attrs($text); + + $self->_do_output_template(); + + $self->{'tree'}->delete(); +} + +sub _do_set_heading_id_attrs +{ + my ($self, $text) = @_; my @headers; my $header; - my @secnums = (0, 0); - my $curlev = -1; - my $newlev; - my $secstr; + my $i = -1; $self->{'tree'} = HTML::TreeBuilder->new(); - $self->{'tree'}->parse_file($file); + $self->{'tree'}->parse($text); + $self->{'tree'}->eof($text); @{$self->{'tree_body'}} = MarkdownBook::HTMLTree::find_elements_by_tag_names( $self->{'tree'}, ('body')); @@ -49,27 +77,11 @@ sub parse @{$self->{'tree_body'}}[0], ('h1', 'h2')); foreach $header (@headers) { - - # Calculate section number. - $newlev = $header->tag(); - $newlev =~ s/^h(\d)$/$1/; - if ($newlev != $curlev) { - foreach (@secnums[$newlev .. $#secnums]) { - $_ = 0; - } - } - $curlev = $newlev; - ++$secnums[$newlev - 1]; - $secstr = join('.', @secnums); - $secstr = $self->{'id'} . '.' . $secstr; - $secstr =~ s/(\.0)*$//; - - push(@{$self->{'sections'}}, - MarkdownBook::Section::HTML->new($self, $secstr, $header)); + $header->attr('id', ${$self->{'sections'}}[++$i]->id()); } } -sub output +sub _do_output_template { my ($self) = @_; -- cgit v0.9.1