繁体   English   中英

用Emacs将非ASCII字符替换为SGML实体代码

[英]Replace non-ASCII characters with SGML entity codes with Emacs

我有一个HTML文件,其中包含一些非ASCII字符,例如用UTF-8或UTF-16编码。 为了将文件保存为ASCII,我想用其(SGML / HTML / XML)实体代码替换它们。 因此,例如,每个ë都应变为ë 每个都应变成◊ 我怎么做?

我使用Emacs作为编辑器。 我确定它具有执行替换的功能,但是我找不到它。 我想念什么? 还是我自己实现它?

我上下搜索,但似乎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))))

我根本不是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))

我认为您正在寻找iso-iso2sgml

有一个字符类 ,其中恰好包含ASCII字符集。 您可以使用与其补语匹配的正则表达式来查找非ASCII字符的出现,然后使用elisp将其替换为其代码:

M-x replace-regexp RET
[^[:ascii:]] RET
\,(concat "&#" (number-to-string (string-to-char \&)) ";") RET

因此,例如,当á匹配时: \\&"á"string-to-char将其转换为 (= 225), number-to-string将其转换为"225" 然后, concat连接"&#""225"";" 得到"&#225;" ,它将替换原始匹配项。

Cx (Cx )包围这些命令,并像往常一样应用Cx Ck nMx insert-kbd-macro来使它们发挥作用。


要查看以交互方式调用此函数的方式,请运行命令,然后按Cx M-:重复复杂命令)。

一个不考虑活动区域的简单版本可能是:

(while (re-search-forward "[^[:ascii:]]" nil t)
  (replace-match (concat "&#"
                         (number-to-string (string-to-char (match-string 0)))
                         ";")))

(这使用推荐的方式以编程方式进行搜索和替换。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM