Skip to content

Instantly share code, notes, and snippets.

@cou929
Last active December 16, 2015 21:39
Show Gist options
  • Save cou929/5501044 to your computer and use it in GitHub Desktop.
Save cou929/5501044 to your computer and use it in GitHub Desktop.
convert trac notaion to textile. forked from: https://github.com/holysugar/trac_wiki_to_textile/blob/master/convert.rb
use strict;
use warnings;
my @lines = <>;
print convert( join '', @lines );
sub convert {
my $text = shift;
$text =~ s/\r//g;
$text =~ s/\{\{\{([^\n]+?)\}\}\}/@$1@/g;
$text =~ s/\{\{\{\n#!([^\n]+?)(.+?)\}\}\}/<pre><code class="$1">$2<\/code><\/pre>/gms;
$text =~ s/\{\{\{(.+?)\}\}\}/<pre>$1<\/pre>/gms;
# macro
$text =~ s/\[\[BR\]\]//g;
$text =~ s/\[\[PageOutline.*\]\]/\{\{toc\}\}/g;
$text =~ s/\[\[Image\((.+?)\)\]\]/!$1!/g;
# header
$text =~ s/=====\s(.+?)\s=====/h5. $1 \n\n/g;
$text =~ s/====\s(.+?)\s====/h4. $1 \n\n/g;
$text =~ s/===\s(.+?)\s===/h3. $1 \n\n/g;
$text =~ s/==\s(.+?)\s==/h2. $1 \n\n/g;
$text =~ s/=\s(.+?)\s=[\s\n]*/h1. $1 \n\n/g;
# table
$text =~ s/\|\|/|/g;
# link
$text =~ s/\[(http[^\s\[\]]+)\s([^\[\]]+)\]/ "$2":$1/g;
$text =~ s/\[([^\s]+)\s(.+)\]/ \[\[$1 | $2\]\] /g;
$text =~ s/([^"\/\!])(([A-Z][a-z0-9]+){2,})/ $1\[\[$2\]\] /g;
$text =~ s/\!(([A-Z][a-z0-9]+){2,})/$1/g;
# text decoration
$text =~ s/'''(.+)'''/\*$1\*/g;
$text =~ s/''(.+)''/_$1_/g;
$text =~ s/`(.+)`/@$1@/g;
# itemize
$text =~ s/^\s\s\s\*/\*\*\*/g;
$text =~ s/^\s\s\*/\*\*/g;
$text =~ s/^\s\*/\*/g;
$text =~ s/^\s\s\s\d\./###/g;
$text =~ s/^\s\s\d\./##/g;
$text =~ s/^\s\d\./#/g;
$text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment