Building: port2wiki.el

File port2wiki.el, 6.3 KB (added by alexey, 11 years ago)

Emacs functions to port things to wiki

Line 
1;; Some emacs functions to make porting documentation to the GHC wiki less painful
2;; Functions intended for direct use are marked with USE
3
4
5;; Note that the function below does non-eager content matching
6;; so that the correct closing tag is chosen.
7;; Otherwise it erroneously picks up two tagged contents on the
8;; same line as one.
9;; Also, the regular expression records the content in register \1
10(defun wk-tag-regexp (tag)
11  "Make a regular expression that matches tagged content."
12  (format "<%s>\\(.*?\\)</%s>" tag tag))
13
14;; USE
15;; The function below can be used to replace
16;;    "<indexterm> ... </indexterm>"
17;; into
18;;    ""
19;; for example.
20;; The arguments in that case should be "indexterm" and ""
21;; Note that above I am quoting the backslash but interactively
22;; it is not needed
23(defun wk-rebracket (tag dest)
24  "Replace the tagged content by the specified result."
25  (interactive "sRebracket content in tag: \nsReplace it by :\n")
26  (query-replace-regexp (wk-tag-regexp tag) dest))
27
28;; USE
29;; This function replaces all content tagged by indexterm with the
30;; empty string.
31(defun wk-kill-idx ()
32  "Remove all occurrences of indexterm content from document."
33  (interactive "")
34  (query-replace-regexp (wk-tag-regexp "indexterm") ""))
35
36;; USE
37;; Replace the given open/closing tags by {{{ /  }}} braces
38;; todo, get the format out of the loop
39;;       and give a message with the number of replacements
40(defun wk-rebrace (str open close)
41  "Replace the tags by the appropriate wiki braces"
42  (interactive "sPick tag to replace by braces : \nsOpening brace : \nsClosing brace : \n")
43  (let ((orig (point)))
44    ;; Replace opening tags
45    ;; The regexp after the tag name is used to catch attributes.
46    (while (re-search-forward (format "<%s.*?>" str) nil t)
47      (replace-match open nil nil))
48    ;; Go back where we started and replace the closing tag.
49    (goto-char orig)
50    (while (re-search-forward (format "</%s>" str) nil t)
51      (replace-match close nil nil))
52    ;; and just go back where we started
53    (goto-char orig)))
54
55;; USE (uses the fun. above to fix many tags)
56;; Rebrace many many tags
57(defun wk-rebrace-many ()
58  "Replace the tags of GHC's docs to trac wiki markup"
59  (interactive "")
60  ;; Rebrace to trac type setting
61  (mapcar (lambda (arg) (wk-rebrace arg "{{{" "}}}"))
62          '("command" "filename" "constant" "function"
63            "literal" "option"))
64  ;; Replace optional things by angle brackets
65  (mapcar (lambda (arg) (wk-rebrace arg "<" ">"))
66          '("optional" "replaceable"))
67  ;; Remove no longer needed tags
68  (mapcar (lambda (arg) (wk-rebrace arg "" ""))
69          '("para" "parameter"))
70  ;; Translate emphasis
71  (mapcar (lambda (arg) (wk-rebrace arg "''" "''"))
72          '("emphasis"))
73  ;; Translate quote
74  (mapcar (lambda (arg) (wk-rebrace arg "\"" "\""))
75          '("quote"))
76  )
77
78;; USE
79;; Useful to do this later
80(defun wk-rebrace-many2 ()
81  "Replace the tags of GHC's docs to trac wiki markup, phase2"
82  (interactive "")
83  ;; Rebrace to trac type setting
84  (mapcar (lambda (arg) (wk-rebrace arg "{{{\n" "\n}}}"))
85          '("screen" "programlisting"))
86  ;; Remove no longer needed tags
87  (mapcar (lambda (arg) (wk-rebrace arg "" ""))
88          '("para" "sect2" "parameter"))
89  )
90
91
92;; buggy
93(defun wk-rebracket-multi (tag dest)
94  "Replace the tagged multiline content by the specified result."
95  (interactive "sRebracket multiline content in tag: \nsReplace it by :\n")
96  ;; The way this works is that the content should be at least one non-new-line
97  ;; character and then several optional new-line followed by characters.
98  (query-replace-regexp (format "<%s>\(.*\(?:\n.+\)*?\)</%s>" tag tag) dest))
99
100;; Helper function
101(defun my-replace (str1 str2)
102  "Replace string"
103  (let ((orig (point)))
104    (while (re-search-forward str1 nil t)
105      (replace-match str2 nil nil))
106    (goto-char orig)
107    ))
108
109;; USE
110;; Replace entities by their represented characters
111(defun wk-repenc ()
112  "Replace some HTML encodings"
113  (interactive)
114  (my-replace "&lowbar;" "_")
115  (my-replace "&dollar;" "$")
116  (my-replace "&ldquo;" "\"")
117  (my-replace "&rdquo;" "\"")
118  (my-replace "&mdash;" "--")
119  (my-replace "&lt;" "<")
120  (my-replace "&gt;" "->")
121  (my-replace "&#60;" "->")
122  )
123
124;; MACROS
125;; Use with care.
126;; they are meant to replace uses of list items with their TRAC wiki counterparts
127;; hard to document, just try them and see if it works for you.
128;; careful, the undo is too fine grained.
129
130;; Note that the macros below assume that C-w is replaced by C-x C-k
131;; it can be achieved by doing:
132;;(global-set-key "\C-c\C-k" 'kill-region)
133
134
135;; Keyboard macro recorded with M-x insert-kbd-macro
136;; after naming it with M-x name-last-kbd-macro
137;;   info from http://www.emacswiki.org/cgi-bin/wiki/KeyboardMacros
138;; This arranges list entries as trac wiki expects
139(fset 'arrange-list-item
140   [?\C-s ?< ?l ?i ?s ?t ?i ?t ?e ?m ?> ?\C-a ?\C-k ?\C-k ?\C-  ?\C-s ?< ?/ ?l ?i ?s ?t ?i ?t ?e ?m ?> ?\C-a ?\C-k ?\C-\M-\\ ?\C-3 ?\C-x tab ?\C-x ?\C-x ?\C-f ?* ?\C-d ?\C-x ?\C-x])
141
142
143;; Not always terribly useful, especially when nested lists and programlistings are involved
144(global-set-key (kbd "C-c l") 'arrange-list-item)
145
146(fset 'arrange-var-list-item
147   [?\C-s ?< ?v ?a ?r ?l ?i ?s ?t ?e ?n ?t ?r ?y ?> ?\C-a ?\C-  ?\C-s ?< ?t ?e ?r ?m ?> ?\C-x ?\C-k ?\C-e ?\C-w backspace backspace ?\C-  ?\C-s ?< ?l ?i ?s ?t ?i ?t ?e ?m ?> ?\C-x ?\C-k ?\C-a ?\C-  ?\C-s ?< ?/ ?l ?i ?s ?t ?i ?t ?e ?m ?> ?\C-a ?\C-\M-\\ ?\C-3 ?\C-x tab ?\C-x ?\C-x ?\C-f ?* ?\C-d ?\C-x ?\C-x ?\C-k ?\C-k ?\C-k ?\C-k])
148
149;; this one is wrong
150;;(fset 'arrange-var-list-item
151;;   [?\C-s ?< ?v ?a ?r ?l ?i ?s ?t ?e ?n ?t ?r ?y ?> ?\C-a ?\C-k ?\C-s ?< ?t ?e ?r ?m ?\M-b ?\C-b ?\M-d ?\C-d ?\C-n ?\C-a ?\C-k ?\C-k ?\C-k ?\C-k ?\C-k ?\C-p ?\C-  ?\C-s ?< ?/ ?l ?i ?s ?t ?i ?t ?e ?m ?> ?\C-a ?\C-\M-\\ ?\C-3 ?\C-x tab ?\C-x ?\C-x ?\C-f ?* ?\C-d ?\C-x ?\C-x ?\C-k ?\C-k ?\C-k ?\C-k])
152
153(global-set-key (kbd "C-c v") 'arrange-var-list-item)
154
155;; Arranges a varlist as a subsubsection (===)
156(fset 'arrange-var-list-item2
157   [?\C-s ?< ?v ?a ?r ?l ?i ?s ?t ?e ?n ?t ?r ?y ?> ?\C-a ?\C-  ?\C-s ?t ?e ?r ?m ?> ?\C-x ?\C-k ?\C-  ?= ?= ?= ?  ?\C-e ?  ?= ?= ?= ?\C-n ?\C-a ?\C-k ?\C-k ?\C-k ?\C-k ?\C-  ?\C-s ?< ?/ ?l ?i ?s ?t ?i ?t ?e ?m ?> ?\C-a ?\C-k ?\C-k ?\C-k ?\C-\M-\\])
158
159(global-set-key (kbd "C-c o") 'arrange-var-list-item2)
160
161