diff --git a/.gen-tags.pl b/.gen-tags.pl new file mode 100755 index 0000000000..cf95ed0711 --- /dev/null +++ b/.gen-tags.pl @@ -0,0 +1,39 @@ +#!/usr/bin/env perl +# +# Generate tags file for explicit and implicit anchors in the Markdown drafts. +# This facilitates jumping to the marked place from the reference. +# +# Usage: ./.gen-tags.pl draft*.md > tags +# +# (In vim, add the dash to the keyword definition via :set iskeyword+=- +# to be able to jump to anchors with dashes in them.) + +sub to_re { + my $search_str = quotemeta shift; + $search_str =~ s/\\\{/{/g; # Revert left curly brace escape just + # performed by quotemeta(). + return $search_str; +} + +while (<>) { + if (/^(#+\s+(.+?))\s*(?:$|\{)/) { # Implicit anchor for header + $re = to_re $1; + $link = $2; + $link =~ y/A-Z/a-z/; + $link =~ s/[^A-Za-z0-9]/-/g; + push @tags, "$link\t$ARGV\t/^$re/\n"; + } + if (/(\{#\s*(.+?)\s*})/) { # Explicit anchor + $re = to_re $1; + $link = $2; + push @tags, "$2\t$ARGV\t/$re/\n"; + } + if (/(\{:\s*#\s*(\S+))/) { # Explicit anchor (for figure) + $re = to_re $1; + $link = $2; + push @tags, "$2\t$ARGV\t/$re/\n"; + } +} + +print "# Generated by $0 at " . localtime . "\n"; +print sort @tags; diff --git a/Makefile b/Makefile index f7521a69a8..cf6e5e01ea 100644 --- a/Makefile +++ b/Makefile @@ -27,3 +27,13 @@ endif show-next: @echo $(drafts_next) + +PERL := $(shell which perl) +ifneq ($(PERL),) +MD_DRAFTS := $(wildcard draft-*.md) +tags:: $(MD_DRAFTS) .gen-tags.pl + $(PERL) .gen-tags.pl $(MD_DRAFTS) > tags + +clean:: + -rm -f tags +endif