I have about 50~ poorly indented and very very hard to read to code. Do you guys have any tips on how to indent this to make it more readable?
-
Most IDEs have an auto-indent hotkey (e.g., alt+shift+f in Net beans). – orb Jul 10 '13 at 12:38
5 Answers
I regularly use a tool called HTML Tidy to clean up dirty HTML. The Ubuntu package is simply called "tidy".
For PHP there seems to exist a tool called phptidy, but apart from testing if it works, I never used it.
- 4,904
- 2
- 24
- 23
-
1Tidy is good but it doesn't support HTML5. Throws an error everytime I use it. – Prathik Rajendran M Jul 11 '13 at 12:10
-
Sorry - I didn't realize that tidy actually hasn't been updated for a very long time... I only work on one website, and that's in HTML 4.01... There exists a fork of tidy with experimental HTML 5 support: http://w3c.github.io/tidy-html5/ – soulsource Jul 11 '13 at 12:29
-
Another downside of `tidy` is that it will alter your tags beyond just whitespace formatting. You can try to set/disable a bunch of options, but there always be unexpected tag changes that sneak through. – wisbucky Jun 21 '18 at 21:24
If you have worked with Python before and the code is HTML or XML, then I would suggest using BeautifulSoup and automate the process for all files. In a Python console run this:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
print(soup.prettify())
Here html_doc is just the text of the file to be indented.
Or the non-geeky was is to use some IDE to get it done individually.
- 135
- 1
- 1
- 9
- 21
- 1
maybe you could try phptidy command line tool which works on Linux and Windows
- 110,292
- 102
- 269
- 299
- 33
- 7
-
Welcome to Ask Ubuntu, please provide more details, this answer will probably be removed as *very low quality*. – Jun 06 '19 at 13:02
PHPFormatter is a php script found here -- https://github.com/NathanWong/phpformatter It can be run via the command line and does an awesome job of formatting PHP,HTML,and Javascript. From the authors description:
"This PHP script will format another PHP script using my arbitrarily set formatting rules (real tabs, 97 chars per line, brackets on their own line, indentation based on operator hierarchy not alignment, spaces around most operators but not commas or brackets, space after if, etc.). It will also format inline HTML, and if JS Beautify is available, it'll use that for inline Javascript in inline HTML too."
It works great in Geany (sudo apt-get install geany). I simply unzipped the 2 files : format.php and jsbeautify.php to my home directory and then added the following custom command in Geany (Edit->Format-Send Selection To->Set Custom Commands):
php /home/myusername/format.php -
(Note the dash at the end. This tells the script to accept input from the standard input stream which is where Geany sends the text.) Now all you have to do is a quick Ctrl+a (SelectAll) then Ctrl+1 (or whichever number you put the command on) and ...
Voila...
Beautifully formatted code!! Thanks Nathan!
- 11
- 1
-
Your link is broken. It looks like NathanWong removed all of his GitHub repositories. Does anyone have an archive of this project? – duckbrain Dec 30 '16 at 23:23
E-tools is a GUI application that indents, formats and highlights HTML, JavaScript, JSON and SQL code. To install the e-tools snap package in all currently supported versions of Ubuntu, open the terminal and type:
sudo snap install e-tools
Highlighting is a nice feature that helps you to scan the code for obvious errors and fix them.
- 110,292
- 102
- 269
- 299