5

I would like to use Textmate's built-in Tidy (Ctrl+Shift+H) functionality to indent my HTML 'without modifying anything' in the code. I write pretty neat HTML already, I just need Tidy to indent my code with Soft-tabs.

Currently it breaks a lot of things and the formatting isn't perfect either. Can someone please write a Tidy config for me that does this:

1 - Indents only, nothing else.

2 - I don't need certain tags to be pushed on a newline. For example: Tidy does this:

<li>
  <a href="#">link</a>
</li>

Original Code (or What I need):

<li><a href="#">link</a></li>

..so if I can pre-define which tags to be kept inline, that would be great.

Basically, I'd like to duplicate Dreamweaver's HTML formatting functionality with Tidy, but the two things mentioned above are really important.

I think this is my current (default?) Tidy config:

${TM_TIDY:-tidy}" -f /tmp/tm_tidy_errors -iq -utf8 \
      -wrap 0 --tab-size $TM_TAB_SIZE --indent-spaces $TM_TAB_SIZE \
    --indent yes \
      ${TM_XHTML:+-asxhtml --output-xhtml yes} \
      ${TM_SELECTED_TEXT:+--show-body-only yes} \
      --enclose-text yes \
      --doctype strict \
    --wrap-php no \
      --tidy-mark no`

I'd greatly appreciate any help. Thanks!

eozzy
  • 66,048
  • 104
  • 272
  • 428

7 Answers7

6

Tidy isn't really meant to do this the way you want to use it. It is not a general-purpose code formatting library, it is a HTML cleanup tool. From the homepage:

Several people have asked if Tidy could preserve the original layout. I am sorry to say that this would be very hard to support due to the way Tidy is implemented. Tidy starts by building a clean parse tree from the source file. The parse tree doesn't contain any information about the original layout.

I really don't think there is a good way to get it to do what you want, but you could play with the whole list of options and see which ones are closer to your ideal.

Zac Thompson
  • 12,401
  • 45
  • 57
2

Try Pretty Diff. It will beautify your code without making any assumptions about your code. You can submit small incomplete fragments of code and Pretty Diff will output this small fragment with indentation.

http://prettydiff.com/?m=beautify&html

austincheney
  • 1,097
  • 7
  • 8
1

In the tiddy config file, set indent option to no.

http://www.w3.org/People/Raggett/tidy/

John M
  • 59
  • 1
1

I hope this to be useful to people stumbling on this page:

If you select the text that you wish to format (aka reindent), TextMate will apply specific actions only to that selection. So, if you CMD+A your document and then apply Tidy (either from the Bundles > HTML menu or with the CTRL+SHIFT+H shortcut) it will indent the selection, skipping alerts on validation which happen when you execute Tidy with no selection. These alerts can sometimes be unnecessary since we often work on page snippets (at least I do).

mcdado
  • 718
  • 1
  • 8
  • 15
0

Changing --indent yes to --indent auto works for me. Its not configurable but seems to handle most cases well.

-1

so, Nimbuz, you also mentioned "Currently it breaks a lot of things" -- you mean it introduces line breaks or it generates HTML that doesn't display properly? Because the latter is happening to me pretty badly, and if you've gotten that to work with a special config, i'd love to see it (trying to do the same thing you want... just formatting). thanks!

-3

Figured out the inline-tags, works now!

eozzy
  • 66,048
  • 104
  • 272
  • 428