IT박스

emacs가 나를 위해 큰 HTML 덩어리를 다시 들여 쓸 수 있습니까?

itboxs 2020. 11. 13. 07:57
반응형

emacs가 나를 위해 큰 HTML 덩어리를 다시 들여 쓸 수 있습니까?


emacs에서 HTML을 편집 할 때 다음과 같이 변경하여 마크 업 블롭을 자동으로 예쁜 형식으로 지정하는 방법이 있습니까?

  <table>
  <tr>
<td>blah</td></tr></table>

... 이에 :

<table>
 <tr>
  <td>
   blah
  </td>
 </tr>
</table>

기본적 .html으로 Emacs (22 또는 23)에서 파일 을 방문하면 html-mode. 그것은 아마도 당신이 원하는 것이 아닐 것입니다. 당신은 아마 원할 것입니다 nxml-mode. nXML 웹 사이트nxml-mode 에서 이전 버전의 emacs 용으로 다운로드 할 수 있지만 Emacs 23에서만 제공되는 것 같습니다 . Debian 및 Ubuntu 패키지도 있습니다. 다음 과 같이 입력 할 수 있습니다 .nxml-modenxml-mode

M-x nxml-mode

다음을 사용하여 nxml 모드 문서를 볼 수 있습니다.

C-h i g (nxml-mode) RET

말한 대로 xhtml 예제를 다시 포맷하려면 Tidy 와 같은 것을 사용해야 할 것입니다 . nxml-mode당신을 얻을 것이다

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head></head>
<body>
<table>
  <tr>
<td>blah</td></tr></table>
</body>

...에

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head></head>
  <body>
    <table>
      <tr>
    <td>blah</td></tr></table>
</body>
</html>

그러나 원하는대로 특정 xml 태그에서 줄 바꿈을 수행하는 더 일반적인 기능을 보지 못했습니다. 참고 C-j짧은 매크로를 수행하거나 해킹 할 수 있습니다 있도록, 적절한 들여 쓰기 새 줄을 삽입합니다 defun테이블을 할 것이다.


당신은 할 수있는 sgml-pretty-print한 후 indent-for-tab같은 지역 / 버퍼에, 당신은 HTML 모드 또는 nxml 모드에 제공했다.

sgml-pretty-print적절한 위치에 새 줄을 indent-for-tab추가하고 멋진 들여 쓰기를 추가합니다. 함께 적절한 형식의 html / xml로 이어집니다.


http://www.delorie.com/gnu/docs/emacs/emacs_277.html

수정하려는 영역을 선택한 후. (전체 버퍼를 선택하려면 Cx h를 사용하십시오)

CMq

하나의 괄호 그룹 (indent-sexp) 내에서 모든 줄을 다시 들여 씁니다.

센티미터-\

영역 (indent-region)의 모든 줄을 다시 들여 씁니다.


i wrote a function myself to do this for xml, which works well in nxml-mode. should work pretty well for html as well:

(defun jta-reformat-xml ()
  "Reformats xml to make it readable (respects current selection)."
  (interactive)
  (save-excursion
    (let ((beg (point-min))
          (end (point-max)))
      (if (and mark-active transient-mark-mode)
          (progn
            (setq beg (min (point) (mark)))
            (setq end (max (point) (mark))))
        (widen))
      (setq end (copy-marker end t))
      (goto-char beg)
      (while (re-search-forward ">\\s-*<" end t)
        (replace-match ">\n<" t t))
      (goto-char beg)
      (indent-region beg end nil))))

You can do a replace regexp

 M-x replace-regexp

 \(</[^>]+>\)

 \1C-q-j

Indent the whole buffer

 C-x h
 M-x indent-region

This question is quite old, but I wasn't really happy with the various answers. A simple way to re-indent an HTML file, given that you are running a relatively newer version of emacs (I am running 24.4.1) is to:

  • open the file in emacs
  • mark the entire file with C-x h (note: if you would like to see what is being marked, add (setq transient-mark-mode t) to your .emacs file)
  • execute M-x indent-region

What's nice about this method is that it does not require any plugins (Conway's suggestion), it does not require a replace regexp (nevcx's suggestion), nor does it require switching modes (jfm3's suggestion). Jay's suggestion is in the right direction — in general, executing C-M-q will indent according to a mode's rules — for example, C-M-q works, in my experience, in js-mode and in several other modes. But neither html-mode nor nxml-mode do not seem to implement C-M-q.


Tidy can do what you want, but only for whole buffer it seems (and the result is XHTML)

M-x tidy-buffer

In emacs 25, which I'm currently building from source, assuming you are in HTML mode, use
Ctrl-x
h

to select all, and then press Tab.


You can pipe a region to xmllint (if you have it) using:

M-|
Shell command on region: xmllint --format -

The result will end up in a new buffer.

I do this with XML, and it works, though I believe xmllint needs certain other options to work with HTML or other not-perfect XML. nxml-mode will tell you if you have a well-formed document.


The easiest way to do it is via command line.

  • Make sure you have tidy installed
  • type tidy -i -m <<file_name>>

Note that -m option replaces the newly tidied file with the old one. If you don't want that, you can type tidy -i -o <<tidied_file_name>> <<untidied_file_name>>

The -i is for indentation. Alternatively, you can create a .tidyrc file that has settings such as

indent: auto
indent-spaces: 2
wrap: 72
markup: yes
output-xml: no
input-xml: no
show-warnings: yes
numeric-entities: yes
quote-marks: yes
quote-nbsp: yes
quote-ampersand: no
break-before-br: no
uppercase-tags: no
uppercase-attributes: no

This way all you have to do is type tidy -o <<tidied_file_name>> <<untidied_file_name>>.

For more just type man tidy on the command line.

참고URL : https://stackoverflow.com/questions/137043/can-emacs-re-indent-a-big-blob-of-html-for-me

반응형