diff options
author | P. J. McDermott <pjm@nac.net> | 2012-08-12 08:34:23 (EDT) |
---|---|---|
committer | P. J. McDermott <pjm@nac.net> | 2012-08-12 08:34:23 (EDT) |
commit | 734dcade6b8df278c95a9c6dfa5e54f076ee04a0 (patch) | |
tree | 4441a51bf1cbf6f7148c11352725244fcd7a5081 | |
parent | 22135732eaa0190ba6ba737d35443cf1d9751c73 (diff) |
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.
-rw-r--r-- | scripts/MarkdownBook/Document/HTML.pm | 64 |
1 files changed, 38 insertions, 26 deletions
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) = @_; |