Source

.emacs.d / site-lisp / anki-to-tex.el

(use-package anki-to-tex
  :commands (my-dictionary-format
             my-format-synonyms)
  :config
  (progn
    (defun my-dictionary-format ()
      (interactive)
      (fix-reset-after-each
       (replace-regexp "" "")
       (replace-regexp "" "")
       (replace-string "E'" "\\`E")
       (replace-regexp "~" "\\\\mytilde{}")
       (replace-regexp "intransitive" "$\\\\iota$")
       (replace-regexp "transitive" "$\\\\tau$")
       (replace-regexp "reflexive" "$\\\\rho$")
       (replace-regexp "auxiliary" "$\\\\alpha$")
       (replace-regexp "impersonal" "$\\\\mu$")
       (replace-regexp "<div><div>" "<div>")
       (replace-regexp "</div></div>" "</div>")
       (replace-regexp "&nbsp;" " ")
       (replace-regexp "&lt;" "<")
       (replace-regexp "&gt;" ">")
       (replace-string "< " "$<$ ")
       (replace-regexp "^<div>" "")
       (replace-regexp "\t</div>" "\t")
       (replace-regexp "<br />" "")
       (replace-regexp "(<i>" "<i>(")
       (replace-regexp "</i>)" ")</i>")
       (replace-regexp "<i>\\(.*?\\)</i>" "\\\\emph{\\1}")
       (replace-regexp "\\([^{]\\)(syn: \\(.*?\\))" "\\1\\\\emph{(syn: \\2)}")
       (replace-regexp "(syn: \\(.*?\\))" "{\\\\footnotesize ($\\\\sigma\\\\!\\\\!:\\\\!$ \\1)}")
       (replace-regexp "\\.\\.\\." "\\\\dots ")
       ;; remove all the divs in front of first tab
       (with-lines
         (goto-char (point-min))
         (save-excursion
           (save-restriction
             (narrow-to-region (point) (save-excursion (re-search-forward "\t" nil t)))
             (replace-string "<div>" "")
             (replace-string "</div>" ""))))
       (while (re-search-forward "\t.*?<div>" nil t)
         (beginning-of-line)
         (re-search-forward "\t" nil t)
         (unless (looking-at "<div>")
           (insert "<div>")
           (re-search-forward "<div>" nil t)
           (backward-char 5)
           (insert "</div>"))
         (end-of-line)
         (unless (looking-back "</div>")
           (insert "</div>")))
       (while (re-search-forward "\t<div>" nil t)
         (backward-delete-char 5)
         (insert "
\\begin{enumerate}
\\item ")
         (end-of-line)
         (backward-delete-char 6)
         (insert "
\\end{enumerate}"))
       (replace-regexp "^\\(.*?\\)\t" "{\\\\bfseries \\1} ")
       (replace-regexp "</div><div>" "
\\\\item ")
       ;; what is this??
       (while (re-search-forward "\\\\item \\[" nil t)
         (backward-delete-char 7)
         (when (looking-back "^")
           (backward-delete-char 1))
         (insert "[")
         (save-excursion
           (save-restriction
             (narrow-to-region (point) (save-excursion (re-search-forward "\\]" nil t)))
             (goto-char (point-min))
             (replace-regexp "
\\\\item " "; "))))
       (while (re-search-forward "\\[" nil t)
         (save-excursion
           (save-restriction
             (narrow-to-region (point) (1- (save-excursion (re-search-forward "\\]" nil t))))
             (goto-char (point-min))
             (insert "{\\footnotesize ")
             (if (re-search-forward "= " nil t)
                 (progn
                   (backward-delete-char 2)
                   (insert "\\emph{")
                   (goto-char (point-max))
                   (insert ";}}"))
               (goto-char (point-max))
               (insert ";}")))))
       (replace-regexp "\\[\\(.*?\\)\\]" "\\\\emph{\\1}")
       (let ((case-fold-search nil)) (replace-string "\\Bf" "\\bf"))
       (my-dictionary-fix-quotes)
       (replace-regexp "$" "
")
       (my-dictionary-remove-single-item-list)
       (replace-string "enumerate" "enumerate*")))

    (defun my-dictionary-fix-quotes ()
      (let ((single 0)
            (double 0))
        (fix-reset-after-each
         (replace-regexp "\\(\\W\\)'\\(\\w\\)" "\\1`\\2")
         ;; (while (re-search-forward "[^a-z]'[^a-z]" nil t)
         ;;   (backward-char 1)
         ;;   (backward-delete-char 1)
         ;;   (if (= (mod double 2) 0)
         ;;       (insert "`")
         ;;     (insert "'"))
         ;;   (setq single (1+ single)))
         (while (re-search-forward "\"" nil t)
           (backward-delete-char 1)
           (if (= (mod double 2) 0)
               (insert "``")
             (insert "''"))
           (setq double (1+ double))))))

    (defun my-dictionary-remove-single-item-list ()
      (goto-char (point-min))
      (while (re-search-forward "\\\\begin{enumerate}" nil t)
        (save-excursion
          (save-restriction
            (narrow-to-region (match-beginning 0) (save-excursion (re-search-forward "\\\\end{enumerate}" nil t)))
            (goto-char (point-min))
            (when (= 1 (count-matches "^\\\\item"))
              (kill-line 2)
              (delete-char 6)
              (end-of-line)
              (delete-region (point) (point-max))))
          (beginning-of-line)
          (backward-delete-char 2)
          (insert " "))))

    (defun my-format-synonyms ()
      (interactive)
      (goto-char (point-min))
      (re-search-forward "Synonyms" nil t)
      (my-kill-entire-line)
      (while (re-search-forward "(\\([0-9]+\\))" nil t)
        (save-excursion
          (let ((text (buffer-substring-no-properties (point) (point-at-eol))))
            (goto-char (point-min))
            (goto-line (string-to-int (match-string 1)))
            (end-of-line)
            (insert " (syn:" text ")")))
        (my-kill-entire-line))
      (goto-char (point-min))
      (replace-regexp "\n\\'" "")
      (clipboard-kill-region (point-min) (point-max)))))