Create Wiki code markup

From CT3

Jump to: navigation, search

This Perl script helps you create proper Wiki markup from source code (Tcl script, EEM applets or router configurations). Currently it only prepends a whitespace in front of each source line, eventually it should handle long lines and wrap them into multiple lines.

Contents

Installation

To use the script, you need a working copy of Perl. It's usually preinstalled on Unix/Linux platforms; Windows version can be downloaded from Activestate. Copy the source code into wikicode.pl and install the file into a directory that's in your PATH.

Unix installation requires two PERL modules: Clipboard and Time::HiRes. As a root, start perl -MCPAN -e shell followed by install Clipboard and install Time::HiRes . The Clipboard.pm module needs the xclip program, which in turn requires libXmu library (on Fedora Linux execute yum install libXmu-devel.i386 ).

If you want to, you can install the Clipboard and Time::HiRes module in the Windows PERL distribution (xclip is not needed on Windows) and use the Unix version of the code on Windows.

Usage guidelines

Usage: wikicode.pl [-o] [-d] [-c] [input_file]

Command line parameters:

  • -o: print the wiki markup on STDOUT. Without the -o parameter the result is copied to the clipboard (ready to be inserted into Wiki editing window);
  • -c: get the input from the clipboard. Without the -c parameter the input is collected from STDIN or you could specify input file in the command line;
  • -d: daemon mode - waits for a new string on the clipboard and immediately transforms it into wiki markup.

Author

Ivan Pepelnjak, © 2008 NIL Data Communications

Source code

#!/usr/bin/perl
use strict;
use Getopt::Std;
use Carp;

our $clipWait = 0;

Use the following code for Windows platforms ...

use Win32::Clipboard;
$clipWait = 3;
our $CLIP = Win32::Clipboard();

sub getClipboard() { return $CLIP->GetText(); }
sub clipboardIsText() { return $CLIP -> IsText(); }

sub setClipboard($) {
  my ($result) = @_ ;
  $CLIP->Empty();
  $CLIP->Set($result);
}

sub waitClipboard() { $CLIP->WaitForChange() }

... and this code for all other platforms:

use Clipboard;
use Time::HiRes;

sub getClipboard() { return Clipboard->paste; }
sub clipboardIsText() { return 1; }
sub setClipboard($) { 
  my($result) = @_ ; 
  Clipboard::Xclip->copy_to_selection('clipboard',$result);
}

sub waitClipboard() {
  my $txt = getClipboard();
  while (1) {
    Time::HiRes::usleep(50000); return if (getClipboard() ne $txt);
  }
} 

The rest of the code is common.

our $lcnt = 0;
our $result = "" ;

our $opt_c;  # accept input from clipboard
our $opt_o;  # display results on stdout
our $opt_d;  # daemon

sub doQuote($) {
  my ($in) = @_ ;

  return " $in\n";
}

sub endQuote() {}

sub quoteInput() {
  my $result;
  while (<>) {
    $result .= doQuote($_) ;
  }
  return $result . endQuote();
}

sub quoteClipboard() {
  my $result;
  if (! clipboardIsText()) {
    
    croak("Clipboard should contain text");
  } else {
    foreach my $line (split /\n/,getClipboard()) {
      $result .= doQuote($line) ;
    }
  }
  return $result . endQuote();
}

sub daemon() {
  my ($result,$timer);

  print "Entering daemon mode. Every text input copied to the clipboard will be quoted immediately\n\n";
  while(1) {
    print "Waiting for a clipboard copy operation\n";
    $timer = time();
    waitClipboard();
    if (clipboardIsText()) {
      if ($timer + $clipWait > time()) { print "Too close together, wait at least $clipWait seconds\n"; next; }
      $lcnt = 0;
      $result = quoteClipboard();
      setClipboard($result);
      print "Result is on the clipboard\n\a";
    } else { print "Not a text copy\n"; }
  }
}

getopts('ocdb');
if ($opt_d) { daemon(); }

$result = $opt_c ? quoteClipboard() : quoteInput();
if ($opt_o) { print $result; } 
  else {
    setClipboard($result);
    print "Result is on the clipboard";
  }