; -*- coding: latin-1 -*-

;--- AN-INDEX:

(defun an ()
  (interactive)
  (message "an-index start...")
  (find-file "~/Eo/kompleks/src/txt/AN-INDEX.txt")

  (goto-char (point-min))
  (while (search-forward "\015" nil t)	; RET
   (delete-char -1))

  (goto-char (point-min))
  (while (re-search-forward "^.*<h[1-4]>.+\n" nil t)
    (replace-match "<new>"))
  
  (goto-char (point-min))
  (while (re-search-forward "^<new> *<b> *<a\\>" nil t)
    (delete-line))
  
  (goto-char (point-min))
  (while (re-search-forward "^<new>[^:]+\n" nil t)
    (replace-match ""))
  
  (goto-char (point-min))
  (while (re-search-forward
	  "^<new> *<b>\\([^:]+\\)</b>\\( ([^)]+)\\)?:? *" nil t)
    (replace-match "<new>\\1 $ \\2###"))
  (message "an-index end."))

;--- Bibliografio:

(defun bib ()
  (interactive)
  (message "bib start...")
  (find-file "~/Eo/kompleks/src/txt/BIB.txt")

  (delete-region
   (point-min)
   (progn (search-forward "<new>") (line-beginning-position)))

  (goto-char (point-min))
  (while (re-search-forward "<h[1-5]>" nil t) (delete-line))

  (goto-char (point-min))
  (while (re-search-forward "<\\(br\\|p\\)>\\|\213 \227\\|\233" nil t)
    (replace-match ""))
  (message "bib end."))

(defun kovrilo ()
  (interactive)
  (message "kovrilo start...")
  (find-file "~/Eo/kompleks/src/txt/KOVRILO.txt")
  (goto-char (point-min))
  (while (re-search-forward "<h[1-5][^>]*>" nil t)
    (replace-match ""))
  (goto-char (point-min))
  (while (re-search-forward "</h[1-5]>" nil t)
    (replace-match "<p>"))
  (message "kovrilo end."))

;--- MLL:

(defun mll ()
  (interactive)
  (message "mll start...")
  (find-file "~/Eo/kompleks/src/txt/VINJETOJ.txt")

  (delete-region
   (point-min)
   (progn (search-forward "<new>") (line-beginning-position)))

  (goto-char (point-min))
  (while (search-forward "<p>" nil t) (replace-match ""))
  (goto-char (point-min))
  (while (re-search-forward "^.*<h[1-5]>.+$" nil t)
    (replace-match "<p>"))

  (goto-char (point-min))
  (while (re-search-forward "<a:#mll>\\([^/]+\\)</a>" nil t)
    (replace-match "\\1")))



;--- TUTO:
(an)
(bib)
(mll)
(kovrilo)
(find-file "~/Eo/kompleks/src/txt/kl0.txt")
(message "elisp-tr start...")
(setq case-replace nil)

(goto-char (point-min))
(while (search-forward "\015" nil t)	; RET
   (delete-char -1))

(goto-char (point-min))
(while (re-search-forward "<\\(new\\|a\\)>«\\([^<»]+\\)»" nil t)
  (replace-match "<\\1>\\2"))

(goto-char (point-min))
(while (re-search-forward "<t[de]\\>[^>]*>" nil t)	; <td colspan=...>
   (replace-match "|"))

(goto-char (point-min))
(insert-buffer "KOVRILO.txt")
(insert-buffer "AN-INDEX.txt")
(insert-buffer "BIB.txt")
(insert-buffer "VINJETOJ.txt")

(delete-trailing-whitespace)		; Emacs-21

(goto-char (point-min))
(while (re-search-forward "  +" nil t)	; squeeze spaces
   (replace-match " "))

(goto-char (point-min))
(while (re-search-forward "<p> *<new>" nil t) (replace-match "<new>"))
; (goto-char (point-min))
; (while (re-search-forward "\012\\(.\\)" nil t)
;    (replace-match " \\1"))
(goto-char (point-min))
(while (re-search-forward "^ +" nil t) (replace-match ""))

(goto-char (point-min)) 		; unuliniigo:
(while
    (progn
      (unless (looking-at "^<new>") (delete-indentation))
      (end-of-line)
      (< (point) (point-max)))
  (forward-line))

(goto-char (point-min))
(while (re-search-forward " *### *\\(<p>\\)?" nil t)
   (replace-match "###"))
(goto-char (point-min))
(while (re-search-forward " *<\\(li\\|p\\|br\\|/?ol\\|/?ul\\)> *" nil t)
   (replace-match "<\\1>"))

(goto-char (point-min))
(while (re-search-forward "<p>\\(<p>\\|<br>\\)+" nil t)
   (replace-match "<p>"))

(goto-char (point-min))
(while (re-search-forward " *\\(<p>\\)? *<hr> *\\(<br>\\)? *" nil t)
   (replace-match "<hr>"))

(goto-char (point-min))
(while (re-search-forward " *<\\(p\\|hr\\|br\\)> *$" nil t)
   (replace-match ""))

(goto-char (point-min))
(while (re-search-forward "</\\(i\\|b\\)>\\( *\\)<\\1>" nil t)
   (replace-match "\\2"))

(goto-char (point-min))
(while (re-search-forward "<a:[^>]*\177[^>]*>" nil t)
  (replace-match "<a>"))

(goto-char (point-min))
(while (re-search-forward "<a:\\([^>]+\\)>\\1\\([^<]*\\)</a>" nil t)
  (replace-match "<a>\\1</a>\\2"))

; Epoc TR is deficient:

(goto-char (point-min))
(while (re-search-forward "\\(</?\\)tt>" nil t) (replace-match "\\1b>"))
(goto-char (point-min))
(while (re-search-forward "\\(</?\\)var>" nil t) (replace-match "\\1i>"))
(goto-char (point-min))
(while (re-search-forward "</?blockquote>" nil t) (replace-match "<hr>"))

; Final:

(goto-char (point-min))
(while (re-search-forward "^<new> *$" nil t) (delete-line))

(message "nun komencigxas ordigo ...")
(while (re-search-forward " *### *" nil t) (replace-match " ###"))
(setq sort-fold-case t)
(sort-lines nil (point-min) (point-max))


(goto-char (point-min))
(while (re-search-forward " *###" nil t) (replace-match "\n"))

(delete-trailing-whitespace)		; Emacs-21

(write-file "~/Eo/kompleks/src/txt/kl.txt")

(message "elisp-tr end.")
