New Pages:
Popular Pages:
Random Pages:
I had a need to allow for single word linking because I am using chiq_chaq for a glossary application where single words are very common. Through my totally amateur hacking, I've been able to get chiq_chaq working to allow single word links by using the relatively standard [[doublebrackets]] seen in other wikis.
Before you look at my code, you may want to demo this functionality for your self. I've set up a demo here:
http://www.swaylocks.com/swaylopedia
I imagine there are much more efficient ways to do what I've done, but then again I'm not really a programmer – just a hacker. If you can think of more efficient ways to do this, feel free to add comments.
To implement single word linking try the following:
In Link.pm:
# Link structure: my $free_link_word = "[-,.()' _0-9A-Za-z]"; # new variable my $link_word = '([^\W_]|\')+'; our $free_link_pattern = "($free_link_word)+"; # new variable our $link_pattern = "$link_word(_$link_word)+";
sub free_internal_link { my ($title) = @_; my $trailer; my $tooltip; # remove [[ ]] brackets $title =~ s/\[\[|\]\]//g; my $anchor = $title; my $attr = ' ' . $tag_attributes if $tag_attributes; my $dir = '‏' if $main::right_to_left; # TODO: ugly $anchor =~ s/_/ /g if ($spaces_in_anchor); if (!page_exists($title)) { return $anchor if ($skip_empty_pages); $trailer = '<sup>?</sup>'; $tooltip = qq( title="$empty_tooltip") if ($empty_tooltip ne ''); } qq(<a href="$pre_url$title$post_url" class="cclink"$attr$tooltip>$anchor$trailer$dir</a>); }
sub link_implicitly { local ($_) = shift; $tag_attributes = shift; s!\[\?(.*)\]!&search_form($1)!eg; # replace_outside_tags($link_pattern, \&internal_link); replace_outside_tags($link_pattern, \&internal_link, 'standard'); # <-- be sure to add the new argument 'standard' # added to allow single word links... replace_outside_tags($free_link_pattern, \&free_internal_link,'free'); $tag_attributes = undef; return $_; }
sub replace_outside_tags { my ($pattern, $replacement_func, $link_type) = @_; # <-- be sure to capture the new argument my $tag = qr(<[^>]*>); my $link = qr((?i)<a[^>]+href\s*=.+?</a>); my $code = qr(((?i)<(pre|span)[^>]+class\s*=\s*"?cccode["\s>](?:\n|.)*?</\2>)); # s!$code|($link)|($tag)|($pattern)!$1||$3||$4||&$replacement_func($5)!egs; if ($link_type eq 'free'){ s!$code|($link)|($tag)|(\[\[$pattern\]\])!$1||$3||$4||&$replacement_func($5)!egs; } else { s!$code|($link)|($tag)|($pattern)!$1||$3||$4||&$replacement_func($5)!egs; } }
sub convert_to_link_pattern { $_[0] =~ s/^\s+//; $_[0] =~ s/\s+$//; $_[0] =~ s/"/''/g; local $_ = $_[0]; return 1 if (/^$link_pattern$/); my $word_separators = '_ \-'; s/[^\'\w$word_separators]//g; # delete illegal characters $_ = join('_', split(/[$word_separators]+/)); # seperate words with _ # return 0 if (!/^$link_pattern$/); $_[0] = $_; return 1; }
In chiq.pl
sub show_search { my ($query, $titles, $pages) = @_; my $html = read_skin('search') or return err("Error reading 'search' skin in $skin_dir: $!"); my $attr = 'target="_blank"' unless $single_browse_window; # format single word result strings.. foreach my $page (@$pages){ if (!($page =~ /_/)){ $page = "[[$page]]\n"; } } foreach my $title (@$titles){ if (!($title =~ /_/)){ $title = "[[$title]]\n"; } } fill_in( $html, {%standard_substitutions, 'words' => $query, 'search_box' => link_implicitly("[\?$query]"), 'titles_num' => scalar(@$titles), 'contents_num' => scalar(@$pages), 'titles' => link_implicitly( join("<br>\n", @$titles), $attr ), 'contents' => link_implicitly( join("<br>\n", @$pages), $attr ), } ); }
I'm pretty sure that's all the changes that are needed. If you have any problems, please put them here, I'll check back or you can email me at mpaler at dontjinxit dot com.
Good luck,
mike
just change the line
our $link_pattern = "$link_word(_$link_word)+";
in Link.pm to
our $link_pattern = "($link_word)?(_$link_word)+";
and you can use a leading underscore for single word links like this : _Eli.
our $link_pattern = "($link_word)?(_$link_word)+";
did not work for me. After modifying Link.pm with that I got the following when I tried to load my Chiq Chaq Home Page:
Chiq_Chaq Error
Illegal page title: Home_Page
Some questions...
Will this solution allow for both link types? (ie single and multiple with underscoress)
Will this solution work with search results pages?