Accept attributes in inline image syntax
[multimarkdown] / Utilities / autocomplete.pl
1 #!/usr/bin/env perl
2 #
3 # $Id: autocomplete.pl 482 2008-01-12 23:07:32Z fletcher $
4 #
5 # Find possible autocompletions in a MultiMarkdown file
6 # ( Designed to be called from TextMate, but the concept can be used elsewhere. )
7 #
8 #
9 # Copyright (c) 2006-2008 Fletcher T. Penney
10 #       <http://fletcherpenney.net/>
11 #
12 # MultiMarkdown Version 2.0.b5
13 #
14
15 # local $/;
16 $word_to_match = $ENV{'TM_CURRENT_WORD'};
17
18 # Read in source file
19 $multimarkdown_file = $ENV{'TM_FILEPATH'};
20
21
22 open(MULTI, "<$multimarkdown_file");
23 local $/;
24 $multi_source = <MULTI>;
25
26 if ( $word_to_match =~ s/^\^/\\^/) {
27
28         # Match Footnotes
29         # the '^' makes things tricky
30
31         $multi_source =~ s/^(?:\[($word_to_match.+?)\]:)?.*$/$1/img;
32         $word_to_match =~ s/\\\^/^/g;
33
34 } elsif ($word_to_match =~ /^\#/) {     
35
36         # Match MultiMarkdown Citations
37
38         $multi_source =~ s/^(?:\[($word_to_match.+?)\]:)?.*$/$1/img;
39
40         #BibTex Citations
41         
42         # Slurp any .bib files
43         open (BIB, "cd $ENV{'TM_DIRECTORY'}; cat *.bib |");
44         local $/;
45         $bibtex = <BIB>;
46         $word_only = $word_to_match;
47         $word_only =~ s/^#//;
48         $bibtex =~ s{
49                 ^(?:@.*?\{($word_only.+?)\,)?.*$
50                 }{
51                         $match = $1;
52                         $match =~ s/(...)/#$1/;
53                         $match;
54                 }xeimg;
55         $multi_source .= $bibtex;
56
57 } else {
58
59         # Match regular anchor (link or image)
60         
61         $multi_source =~ s{
62                 ^(?:
63                         (?:\#{1,6}\s*|\[)
64                         ($word_to_match.+?)                                     # Match Heading
65                         (?:\]:|\s*\#)
66                 )?
67                 (?:.*\[($word_to_match.+?)\]>>)?                # Match Equation label
68                 (?:\[.*?\]\[($word_to_match.+?)\])?             # Match Table label (or at least a label
69                 .*?$                                                                    #       at the beginning of a line)
70         }{
71                 "$1$2$3";
72         }gemix;
73 }
74
75 # Strip blank lines
76 $multi_source =~ s/\n\s*\n/\n/gs;
77
78 # Fix case (TextMate won't autocomplete different cases)
79 if ($word_to_match =~ /^\^/) {
80         $multi_source =~ s/\^+$word_to_match/$word_to_match/igm;
81 } else {
82         $multi_source =~ s/^$word_to_match/$word_to_match/igm;
83 }
84
85 # Print
86
87 print $multi_source;