misc:software:Emacs:各種スクリプト:とある平文テキストの変換処理(文章)

misc:software:Emacs:各種スクリプト:とある平文テキストの変換処理(文章)

(let ((nbf (generate-new-buffer "*TEMP*")))
  (with-current-buffer nbf
    (unwind-protect
        (let* (
               (view? nil)
               (write-file? t)
               (output-file "z:/output.xml")
               (dir "z:/")
               (file-list '("z:/input.txt"))
               ;; (loop for f in (directory-files dir t)
               ;;       when (string= "txt" (substring f -3))
               ;;       collect f))
               )
          (dolist (file file-list)
            (insert-file-contents file)
            )

          (goto-char (point-min))
          (replace-string "\r" "")

          ;; <article></article>
          ;; <h2></h2>
          (save-match-data
            (goto-char (point-min))
            (search-forward-regexp "^[#][^#]")
            (let ((text (buffer-substring (line-beginning-position) (line-end-position))))
              (forward-line 1)
              (delete-region (match-beginning 0) (point))

              (insert "<?xml version=\"1.0\" encoding=\"utf-8\" ?>")
              (newline)
              (insert "<article>")
              (newline)
              (insert "<article_head>")
              (newline)
              (insert "<article_addr><sect>hirayama</sect></article_addr>")
              (newline)
              (insert (concat "<article_subj>" (substring text 1) "</article_subj>"))
              (newline)
              (insert "</article_head>")
              (newline)
              (insert "<article_body>")
              (newline)

              (insert "<h2 id=\"text_info\">")
              (insert "書誌情報")
              (insert "</h2>")
              (newline)

              (insert "<h2>本文</h2>")
              (goto-char (point-max))
              (newline)
              (insert "</article_body>")
              (newline)
              (insert "</article>")
              ))


          ;; <h3></h3>
          (goto-char (point-min))
          (while (re-search-forward "^##\\([^#]+?\\)$" nil t)
            (replace-match "<h3>\\1</h3>" nil nil))

          ;; <h4></h4>
          (goto-char (point-min))
          (while (re-search-forward "^###\\([^#]+?\\)$" nil t)
            (replace-match "<h4>\\1</h4>" nil nil))


          (goto-char (point-min))
          (while (re-search-forward "^\\([^<\n].+?\\)$" nil t)
            (replace-match "<p>\\1</p>" nil nil))
          
          (if view?
              (pop-to-buffer nbf)
            (if write-file?
                (write-region (point-min) (point-max) output-file)
              (clipboard-kill-ring-save (point-min) (point-max))))
          ))))

Last modified : 2013/06/18 02:02:14 JST