summaryrefslogtreecommitdiffstats
path: root/scripts/MarkdownBook/Book.pm
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 /scripts/MarkdownBook/Book.pm
parenteda7d670f3545bbf0a5704a19920417c71d25e60 (diff)
Support macro substitutions.
Diffstat (limited to 'scripts/MarkdownBook/Book.pm')
-rw-r--r--scripts/MarkdownBook/Book.pm56
1 files changed, 56 insertions, 0 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) = @_;