Mic*_*ter 2 html xml emacs sgml html-entities
我有一个包含一些非 ASCII 字符的 HTML 文件,例如以 UTF-8 或 UTF-16 编码的字符。为了以 ASCII 保存文件,我想用它们的 (SGML/HTML/XML) 实体代码替换它们。例如,每个都\xc3\xab应该成为ë,每个都\xe2\x97\x8a应该成为◊。我怎么做?
我使用 Emacs 作为编辑器。我确信它有一个功能可以进行替换,但我找不到它。我缺少什么?或者我自己如何实现?
\n我到处搜索,但似乎Emacs(或至少24.3.1版本)没有这样的功能。我也无法在某处找到它。
基于我确实找到的类似(但不同)的功能,我自己实现了它:
(require 'cl)
(defun html-nonascii-to-entities (string)
"Replace any non-ascii characters with HTML (actually SGML) entity codes."
(mapconcat
#'(lambda (char)
(case char
(t (if (and (<= 8 char)
(<= char 126))
(char-to-string char)
(format "&#%02d;" char)))))
string
""))
(defun html-nonascii-to-entities-region (region-begin region-end)
"Replace any non-ascii characters with HTML (actually SGML) entity codes."
(interactive "r")
(save-excursion
(let ((escaped (html-nonascii-to-entities (buffer-substring region-begin region-end))))
(delete-region region-begin region-end)
(goto-char region-begin)
(insert escaped))))
Run Code Online (Sandbox Code Playgroud)
我根本不是 Elisp 专家,但这确实有效!
我还发现find-next-unsafe-char很有价值。
编辑:互动版本!
(defun query-replace-nonascii-with-entities ()
"Replace any non-ascii characters with HTML (actually SGML) entity codes."
(interactive)
(perform-replace "[^[:ascii:]]"
`((lambda (data count)
(format "&#%02d;" ; Hex: "&#x%x;"
(string-to-char (match-string 0)))))
t t nil))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1115 次 |
| 最近记录: |