summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pjm@nac.net>2012-08-12 07:50:04 (EDT)
committer P. J. McDermott <pjm@nac.net>2012-08-12 07:50:04 (EDT)
commitb8ea4e24bf2c177f7c51861d1a33166f912082c5 (patch)
tree483845610c686a6c1a2ef8119855aa4921242085
parenteda7d670f3545bbf0a5704a19920417c71d25e60 (diff)
Support macro substitutions.
-rw-r--r--scripts/MarkdownBook/Book.pm56
-rw-r--r--scripts/MarkdownBook/Document.pm2
-rw-r--r--scripts/MarkdownBook/Document/Txt.pm2
-rw-r--r--scripts/MarkdownBook/Section.pm10
4 files changed, 67 insertions, 3 deletions
diff --git a/scripts/MarkdownBook/Book.pm b/scripts/MarkdownBook/Book.pm
index 3328cae..bb33f9f 100644
--- a/scripts/MarkdownBook/Book.pm
+++ b/scripts/MarkdownBook/Book.pm
@@ -152,6 +152,62 @@ sub add_section
$self->{'sections_by_id'}->{$section->id()} = $section;
}
+sub subst_macros
+{
+ my ($self, $text) = @_;
+
+ $text =~ s/
+ \$ # Dollar sign
+ \[ # Left square bracket
+ ([^\]]+) # Macro
+ \] # Right square bracket
+ (
+ \[ # Left square bracket
+ ([^\]]+) # Macro parameters
+ \] # Right square bracket
+ )
+ /$self->_do_subst_macro($1, split(m@[ \t]+@, $3))/exg;
+ $text =~ s/
+ \$ # Dollar sign
+ \[ # Left square bracket
+ ([^\]]+) # Macro
+ \] # Right square bracket
+ /$self->_do_subst_macro($1)/exg;
+
+ return $text;
+}
+
+sub _do_subst_macro
+{
+ my ($self, $macro, @params) = @_;
+
+ if ($macro eq 'toc') {
+ return $self->_do_gen_toc();
+ } elsif ($macro eq 'sectlink') {
+ } else {
+ die("Unrecognized macro \"$macro\"");
+ }
+}
+
+sub _do_gen_toc
+{
+ my ($self) = @_;
+ my $section;
+ my $toc = '';
+
+ # TODO: Add chapters and appendices as well.
+ foreach $section (@{$self->{'sections'}}) {
+ $toc .= "\n" if $toc ne '';
+ $toc .= ' ' x $section->level();
+ $toc .= '* ';
+ $toc .= $section->number();
+ $toc .= ' ';
+ $toc .= $section->title();
+ }
+
+ return $toc;
+}
+
sub parse
{
my ($self) = @_;
diff --git a/scripts/MarkdownBook/Document.pm b/scripts/MarkdownBook/Document.pm
index 63750f3..9bb98c5 100644
--- a/scripts/MarkdownBook/Document.pm
+++ b/scripts/MarkdownBook/Document.pm
@@ -211,7 +211,7 @@ sub _do_header
/$1/x;
$section = MarkdownBook::Section->new($self,
- $section_number, $section_id, $section_title);
+ $level, $section_number, $section_id, $section_title);
push(@{$self->{'sections'}}, $section);
$self->{'book'}->add_section($section);
diff --git a/scripts/MarkdownBook/Document/Txt.pm b/scripts/MarkdownBook/Document/Txt.pm
index 4f30d43..e03a1bb 100644
--- a/scripts/MarkdownBook/Document/Txt.pm
+++ b/scripts/MarkdownBook/Document/Txt.pm
@@ -33,7 +33,7 @@ sub output
print($out_fh $self->{'title'} . "\n" .
'*' x length($self->{'title'}) . "\n\n\n");
- print($out_fh $self->{'source_text'});
+ print($out_fh $self->{'book'}->subst_macros($self->{'source_text'}));
close($out_fh);
}
diff --git a/scripts/MarkdownBook/Section.pm b/scripts/MarkdownBook/Section.pm
index c19a0d0..39b29eb 100644
--- a/scripts/MarkdownBook/Section.pm
+++ b/scripts/MarkdownBook/Section.pm
@@ -20,7 +20,7 @@ package MarkdownBook::Section;
sub new
{
- my ($class, $doc, $num, $id, $title) = @_;
+ my ($class, $doc, $lev, $num, $id, $title) = @_;
my $self;
$class = ref($class) || $class;
@@ -28,6 +28,7 @@ sub new
bless($self, $class);
$self->{'document'} = $doc;
+ $self->{'level'} = $lev;
$self->{'number'} = $num;
$self->{'id'} = $id;
$self->{'title'} = $title;
@@ -35,6 +36,13 @@ sub new
return $self;
}
+sub level
+{
+ my ($self) = @_;
+
+ return $self->{'level'};
+}
+
sub number
{
my ($self, $number) = @_;