From b8ea4e24bf2c177f7c51861d1a33166f912082c5 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sun, 12 Aug 2012 07:50:04 -0400 Subject: Support macro substitutions. --- (limited to 'scripts/MarkdownBook/Book.pm') 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) = @_; -- cgit v0.9.1