From b3fc81396147a4df176cd86cbf0e42e73fdc418f Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Fri, 10 Aug 2012 14:29:35 -0400 Subject: Rewrite section code. --- diff --git a/scripts/MarkdownBook/Book.pm b/scripts/MarkdownBook/Book.pm index a0cb49e..77193fd 100644 --- a/scripts/MarkdownBook/Book.pm +++ b/scripts/MarkdownBook/Book.pm @@ -65,6 +65,16 @@ sub title return $old; } +sub documents +{ + my ($self, $documents) = @_; + my $old = $self->{'docs'}; + + $self->{'docs'} = $documents if defined($documents); + + return $old; +} + sub get_document_module { my ($self, $mod) = @_; diff --git a/scripts/MarkdownBook/Book/HTML.pm b/scripts/MarkdownBook/Book/HTML.pm index 8577170..662b347 100644 --- a/scripts/MarkdownBook/Book/HTML.pm +++ b/scripts/MarkdownBook/Book/HTML.pm @@ -49,16 +49,6 @@ sub parse_documents } } -sub number_sections -{ - my ($self) = @_; - my $doc; - - foreach $doc (@{$self->{'docs'}}) { - $doc->number_sections(); - } -} - sub write_templated_documents { my ($self) = @_; diff --git a/scripts/MarkdownBook/Document/HTML.pm b/scripts/MarkdownBook/Document/HTML.pm index ce19f45..a7e901a 100644 --- a/scripts/MarkdownBook/Document/HTML.pm +++ b/scripts/MarkdownBook/Document/HTML.pm @@ -17,6 +17,7 @@ use strict; use warnings; use MarkdownBook::Document; +use MarkdownBook::Section::HTML; package MarkdownBook::Document::HTML; @@ -26,24 +27,15 @@ sub parse_html { my ($self) = @_; my $file = $self->{'book'}->dir() . '/' . $self->{'file'} . '.html.in'; - - $self->{'tree'} = HTML::TreeBuilder->new(); - $self->{'tree'}->parse_file($file); -} - -sub number_sections -{ - my ($self) = @_; - my @headers; my $header; my @secnums = (0, 0); my $curlev = -1; my $newlev; my $secstr; - my @children; - my $sectitle; - my $secid; + + $self->{'tree'} = HTML::TreeBuilder->new(); + $self->{'tree'}->parse_file($file); @{$self->{'tree_body'}} = MarkdownBook::HTMLTree::find_elements_by_tag_names( $self->{'tree'}, ('body')); @@ -65,25 +57,20 @@ sub number_sections $secstr = join('.', @secnums); $secstr =~ s/(\.0)*$//; - MarkdownBook::HTMLTree::each_text(sub { - - # Prefix section title with section number. - my $sectitle = $_[1]; - $sectitle =~ s/^([^\[]+)[ ]*\[[^\]]+\]$/$_[2].$secstr $1/; - - # Set "id" attribute. - if ($_[1] =~ m/^[^\[\]]*\[([^\]]+)\][^\[\]]*$/) { - my $secid = $_[1]; - $secid =~ s/^[^\[\]]*\[([^\]]+)\][^\[\]]*$/$1/; - ${$_[0]}->attr('id', $secid); - } - - # Set section title. - $_[1] = $sectitle; + push(@{$self->{'sections'}}, + MarkdownBook::Section::HTML->new($self, $secstr, $header)); + } +} - }, $header, $self->{'id'}); +sub sections +{ + my ($self) = @_; + # FIXME: Why is this necessary?! + foreach my $sec (@{$self->{'sections'}}) { } + + return $self->{'sections'}; } sub output diff --git a/scripts/MarkdownBook/HTMLTree.pm b/scripts/MarkdownBook/HTMLTree.pm index 27db3e1..419eb53 100644 --- a/scripts/MarkdownBook/HTMLTree.pm +++ b/scripts/MarkdownBook/HTMLTree.pm @@ -43,30 +43,30 @@ sub find_elements_by_tag_names sub each_text { - my ($code, $elem, @args) = @_; + my ($elem) = @_; my $child = $elem; my $root = $elem; - _each_text_recursive($code, $child, $root, @args); + return _each_text_recursive($child, $root); } sub _each_text_recursive { - my ($code, $elem, $root, @args) = @_; + my ($elem, $root) = @_; my $child; - my $i = -1; + my @retlist = (); foreach $child ($elem->content_list()) { - ++$i; if (ref($_) eq 'HTML::Element') { - _each_text_recursive($code, $child, $root, @args); + push(@retlist, _each_text_recursive($child, $root)); } else { - $code->(\$root, $child, @args); - $elem->splice_content($i, 1, $child); + push(@retlist, $child); } } + + return @retlist; } 1; diff --git a/scripts/MarkdownBook/Section.pm b/scripts/MarkdownBook/Section.pm new file mode 100644 index 0000000..7eb441c --- /dev/null +++ b/scripts/MarkdownBook/Section.pm @@ -0,0 +1,51 @@ +# Copyright (C) 2012 Patrick "P. J." McDermott +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +use strict; +use warnings; + +package MarkdownBook::Section; + +sub number +{ + my ($self, $number) = @_; + my $old = $self->{'number'}; + + $self->{'number'} = $number if defined($number); + + return $old; +} + +sub id +{ + my ($self, $id) = @_; + my $old = $self->{'id'}; + + $self->{'id'} = $id if defined($id); + + return $old; +} + +sub title +{ + my ($self, $title) = @_; + my $old = $self->{'title'}; + + $self->{'title'} = $title if defined($title); + + return $old; +} + +1; diff --git a/scripts/MarkdownBook/Section/HTML.pm b/scripts/MarkdownBook/Section/HTML.pm new file mode 100644 index 0000000..8208d9e --- /dev/null +++ b/scripts/MarkdownBook/Section/HTML.pm @@ -0,0 +1,66 @@ +# Copyright (C) 2012 Patrick "P. J." McDermott +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +use strict; +use warnings; + +use HTML::Element; + +package MarkdownBook::Section::HTML; + +our @ISA = qw(MarkdownBook::Section); + +sub new +{ + my ($class, $doc, $num, $elem) = @_; + my $self; + my $child; + my $i = -1; + my $secid; + my $sectitle; + + $class = ref($class) || $class; + $self = {}; + bless($self, $class); + + $self->{'document'} = $doc; + $self->{'number'} = $num; + $self->{'element'} = $elem; + + foreach $child (MarkdownBook::HTMLTree::each_text($elem)) { + + ++$i; + + # Set "id" attribute. + if ($child =~ m/^[^\[\]]*\[([^\]]+)\][^\[\]]*$/) { + $secid = $child; + $secid =~ s/^[^\[\]]*\[([^\]]+)\][^\[\]]*$/$1/; + $elem->attr('id', $secid); + } + + # Prefix section title with section number. + $child =~ s/^([^\[]+)[ ]*\[[^\]]+\]$/$1/; + $child =~ s/[ ]*$//; + + # Set section title. + $sectitle .= $child; + $elem->splice_content($i, 1, $num . ' ' . $child); + + } + + return $self; +} + +1; diff --git a/scripts/postproc.pl b/scripts/postproc.pl index e9bee91..3d8cbe9 100755 --- a/scripts/postproc.pl +++ b/scripts/postproc.pl @@ -29,5 +29,4 @@ $book = MarkdownBook::Book::HTML->new($doc_dir); $book->create_documents(); $book->parse_documents(); -$book->number_sections(); $book->write_templated_documents(); -- cgit v0.9.1