Added section on rename and copy detection
[gitmagic/gitmagic.git] / wiki2xml
blobe943aa08e0d0a603f4312933d3ae7754afc16e95
1 #!/usr/bin/gawk -f
2 # Converts variant of MediaWiki to DocBook
3 # Ben Lynn
5 func open_section(tag) {
6 stack[stack_i] = tag
7 stack_i = stack_i + 1
8 print "<"tag">"
11 func close_section() {
12 close_block();
13 stack_i = stack_i - 1
14 print "</"stack[stack_i]">"
17 func close_until(i) {
18 close_lists()
19 while (stack_i > i) {
20 close_section()
24 func close_block() {
25 if (block_state != "") {
26 print "</"block_state">"
27 block_state = ""
31 func in_block(s) {
32 if (block_state != s) {
33 close_block()
34 print "<"s">"
35 block_state = s
39 func ul_item() {
40 if (in_list) {
41 close_block()
42 print "</listitem>"
43 } else {
44 in_list = 1
45 print "<itemizedlist>"
47 print "<listitem>"
50 func close_lists() {
51 if (in_list) {
52 close_block()
53 print "</listitem>"
54 print "</itemizedlist>"
55 in_list = 0
56 list_indent = 0
60 BEGIN {
61 stack_i = 0
64 # empty lines = close block
65 /^ *$/ {
66 close_block()
67 next
70 # lists/stuff that goes in <screen> tags
71 # if matches a previous indentation level it's a list
72 # otherwise it should have higher indentation level
73 # and belongs in <screen> tag
75 /^ / {
76 n = length()
77 # must use gensub because length() doesn't seem to change after sub()
78 tmp = gensub("^ *", "", 1)
79 n -= length(tmp)
80 $0 = tmp
81 if (list_indent == n) {
82 was_list_item = 1
83 } else if (list_indent < n) {
84 in_block("screen")
85 print $0
86 next
87 } else {
88 print "funny indent level"
89 print $0
90 next
94 /^-/ {
95 was_list_item = 1
96 n = length()
97 # must use gensub because length() doesn't seem to change after sub()
98 tmp = gensub("^- *", "", 1)
99 $0 = tmp
100 n -= length(tmp)
101 list_indent = n
102 ul_item()
105 # replace " with prettier Unicode open/close double quotes
106 /"/ {
107 while (match($0, "\"")) {
108 sub("\"", "\\&#8220;")
109 sub("\"", "\\&#8221;")
113 /''/ {
114 while (match($0, "''")) {
115 sub("''", "<emphasis>")
116 sub("''", "</emphasis>")
120 # headings
122 /^ *===.*===$/ {
123 gsub(" *=== *","")
124 close_until(2)
125 open_section("section")
126 print "<title>"$0"</title>"
127 next
130 /^ *==.*==$/ {
131 gsub(" *== *","")
132 close_until(1)
133 open_section("section")
134 print "<title>"$0"</title>"
135 next
138 /^ *=.*=$/ {
139 gsub(" *= *","")
140 close_until(0)
141 open_section("chapter")
142 print "<title>"$0"</title>"
143 next
146 # links
147 /\[\[/ {
148 gsub("\\[\\[", "<ulink url=\"")
149 gsub("]\\[", "\">")
150 gsub("]]", "</ulink>")
153 # default = paragraph
155 if (!was_list_item) {
156 close_lists()
158 was_list_item = 0
159 in_block("para")
160 print $0
163 END {
164 close_until(0)