summaryrefslogtreecommitdiffstats
path: root/scripts/MarkdownBook/Book.pm
diff options
context:
space:
mode:
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) = @_;