WikiTips2Code

From PsiWiki

Contents

About

TODO: maybe this should be put into Darcs for following better the versioning - needs to be discussed

This is a script to convert the tips found in Tips and Tricks page into C++/Qt code to be included in the Psi sources.

There are two files in this package:

  • codetips - a bash script doing all the work
  • codetips.rules - a file with sed instructions

Don't forget to do

chmod +x codetips

Usage

First, you need to get the Wiki code describing the tips. It can be found in the source of Tips and Tricks between

<!-- Tips: START CUT -->
...
<!-- Tips: END CUT - ADD ABOVE THIS LINE, USING THE PATTERN -->

Save that wiki code to a file - let consider it tips.txt.

At the command prompt do

./codetips tips.txt code.cpp

code.cpp will contain the exact C++/Qt code necessary to be inserted in the Psi sources.

Script

codetips

#!/bin/bash

# CodeTips v0.1
# Copyright 2005 - Mircea Bardac (dev AT mircea.bardac.net)
# Licensed under GPL v2

codefile=$2
tmpfile=codetips.tmp

echo -n > $codefile # new output file
contributor=""

cat $1 | grep -v "^[ \t\n]*$" | while read cline; do
	t1=$(echo "$cline" | sed "s|^<div style=.*>||")
	if test -z "$t1"
	then
		# new tip started
		echo -n > $tmpfile  # clear temporary file
		contributor=""
		continue
	fi
	
	t2=$(echo "$cline" | sed "s|^<br /><br /><em>.*</em>||")
	if test -z "$t2"
	then
		# found contributor
		contributor=$(echo "$cline" | sed "s|^<br /><br /><em>Contributed by \(.*\)</em>|\1|g")
		continue
	fi
	
	t3=$(echo "$cline" | sed "s|^</div>.*||")
	if test -z "$t3"
	then
		# tip ended
		
		# escaping - could be moved out into a separate file in the future
		sed -i -f codetips.rules $tmpfile
		
		# writing code
		linecount=$(cat $tmpfile | wc -l)
		echo -n -e "\taddTip( tr(" >> $codefile
		lineno=0
		cat $tmpfile | while read tipline; do
			codeline=$tipline
			let "lineno = lineno + 1"
			[ $lineno -lt $linecount ] && codeline="$codeline\\\\n"
			codeline="\"$codeline\""
			[ $lineno -ne 1 ] && codeline="\t$codeline"
			[ $lineno -eq $linecount ] && codeline="$codeline),"
			codeline="\t$codeline"
			echo -e $codeline >> $codefile
		done
		echo -e "\t\t\"$contributor\" );" >> $codefile
		continue
	fi
	
	# we are in the tip contents
	echo "$cline" >> $tmpfile
	
done

rm $tmpfile

codetips.rules

s|"|\\\\\\\"|g
s|:)|<icon name=\\\\\\\"psi/smile\\\\\\\">|g
s|\[\(http://[^ ]*\)[ |]\([^]]*\)\]|<a href=\\\\\"\1\\\\\">\2</a>|g