EzDevInfo.com

Tidy

Tidy - A CSS framework without the mess

How to tidy up R code?

I know that many programming languages have tools to tidy up programming code? Is there a way to tidy up R code or some tools that will do this (e.g. included in R IDE's?)?


Source: (StackOverflow)

Tidy for Jinja2 templates

Is there a tidy-like tool or a tidy configuration, which works fine with Jinja2 templates? The default tidy has problems with Jinja code in attributes and Jinja-loops are formated in an unreadable way. My main requirement is to get the indentation level right. Everything else is nice to have, but not required.


Source: (StackOverflow)

Advertisements

Formatting PHP Code within Vim

I'm currently using Vim as a lightweight IDE. I have NERDTree, bufexplorer, supertab, and ctags plugins which do almost everything I want. Only big thing missing for me is auto code formatting.

I'm working with some messy PHP code which has inconsistent indenting and code formatting, ideally I could highlight the code I want formatted (whole files would be fine too) and run a command to tidy it.

Does anybody have a nice solution for this working in Vim?


Source: (StackOverflow)

PHP generated XML shows invalid Char value 27 message

I am generating XML using PHP library as below:

$dom = new DOMDocument("1.0","utf-8");

Doing above results in a page which shows a message on top of the output.

This page contains the following errors: error on line 16 at column 274505: PCDATA invalid Char value 27 Below is a rendering of the page up to the first error.

I have tried rectifying using Tidy library.. used iconv to get the chinese character in UTF-8.

I still see the above. Please help me solve this error. Thanks!


Source: (StackOverflow)

Screenscraping the ugliest HTML you've ever seen in your life

I'm using PHP and libtidy to attempt to screen scrape what might possibly be the most horrendous and malformed use of HTML tables in history. The site closes few table, tr, td, font, or bold tags and consistently nests many different layers of tables within tables.

Example snippet:

<center>
<table border="1" bordercolor="#000000" cellspacing="0" cellpadding="0">
<tr>
<td width="50%">
<center>
Home Team - <b>Wildcats<td>
<center>
Away Team - <b>Polar Bears<tr>
<td colspan="2">
<center>
<b><font size="+1">Rosters<tr>
<td valign="top">
<center>
<table border="0" cellspacing="0">
<tr>
<td>
<font size="2">1&nbsp;<td>
<font size="2">Baird, T<tr>
<td>
<font size="2">2&nbsp;<td>
<font size="2">Knight, P<tr>
<td>
<font size="2">8&nbsp;<td>
<font size="2">Miller, B<tr>
<td>
<font size="2">9&nbsp;<td>
<font size="2">Huebsch, B<tr>
<td>
<font size="2">11&nbsp;<td>
<font size="2">Buschmann, C<tr>
<td>
<font size="2">12&nbsp;<td>
<font size="2">Reding, J<tr>
<td>
<font size="2">14&nbsp;<td>
<font size="2">Simpson, S<tr>
<td>
<font size="2">27&nbsp;<td>
<font size="2">Kupferschmidt, M<tr>
<td>
<font size="2">28&nbsp;<td>
<font size="2">Anderson, D<tr>
<td>
<font size="2">31&nbsp;<td>
<font size="2">Gehrts, J<tr>
<td>
<font size="2">39&nbsp;<td>
<font size="2">McGinnis, G<tr>
<td>
<font size="2">42&nbsp;<td>
<font size="2">Temple, B<tr>
<td>
<font size="2">44&nbsp;<td>
<font size="2">Kemplin, A<tr>
<td>
<font size="2">77&nbsp;<td>
<font size="2">Weiner, B<tr>
<td>
<font size="2">95&nbsp;<td>
<font size="2">
Zytkoskie, D</table>
<td valign="top">
<center>
<table border="0" cellspacing="0">
<tr>
<td>
<font size="2">5&nbsp;<td>
<font size="2">Mack, A<tr>
<td>
<font size="2">8&nbsp;<td>
<font size="2">Foucault, R<tr>
<td>
<font size="2">11&nbsp;<td>
<font size="2">Oberpriller, D *<tr>
<td>
<font size="2">12&nbsp;<td>
<font size="2">Underwood, J<tr>
<td>
<font size="2">15&nbsp;<td>
<font size="2">Oberpriller, M<tr>
<td>
<font size="2">19&nbsp;<td>
<font size="2">Langfus, B<tr>
<td>
<font size="2">25&nbsp;<td>
<font size="2">Carroll, R<tr>
<td>
<font size="2">30&nbsp;<td>
<font size="2">Hirdler, T<tr>
<td>
<font size="2">33&nbsp;<td>
<font size="2">Gibson, S<tr>
<td>
<font size="2">35&nbsp;<td>
<font size="2">Marthaler, C<tr>
<td>
<font size="2">44&nbsp;<td>
<font size="2">Yurik, J<tr>
<td>
<font size="2">58&nbsp;<td>
<font size="2">
Gronemeyer, S</table>
<tr>
<td colspan="2">
<center>
<b><font size="+1">Goals<tr>
<td valign="top">
<center>
<table border="1" cellspacing="0" width="100%">
<td>
<b><font size="2">Player<td>
<b><font size="2">Period<td>
<b><font size="2">Time<td>
<b><font size="2">Assist 1<td>
<b><font size="2">Assist 2<td>
<b><font size="2">SH<td>
<b><font size="2">PP<tr>
<td nowrap>
<font size="2">Kupferschmidt,&nbsp;M<td>
<font size="2">1<td>
<font size="2">12:51<td nowrap>
<font size="2">Kemplin,&nbsp;A<td nowrap>
<font size="2">None<td>
<font size="2">
<center>
<td>
<font size="2">
<center>
<tr>
<td nowrap>
<font size="2">McGinnis,&nbsp;G<td>
<font size="2">1<td>
<font size="2">12:33<td nowrap>
<font size="2">Huebsch,&nbsp;B<td nowrap>
<font size="2">None<td>
<font size="2">
<center>
<td>
<font size="2">
<center>
<tr>
<td nowrap>
<font size="2">Kupferschmidt,&nbsp;M<td>
<font size="2">2<td>
<font size="2">16:01<td nowrap>
<font size="2">None<td nowrap>
<font size="2">None<td>
<font size="2">
<center>
<td>
<font size="2">
<center>
<tr>
<td nowrap>
<font size="2">Buschmann,&nbsp;C<td>
<font size="2">3<td>
<font size="2">00:38<td nowrap>
<font size="2">None<td nowrap>
<font size="2">None<td>
<font size="2">
<center>
<td>
<font size="2">
<center>
</table>
<td valign="top">
<center>
<table border="1" cellspacing="0" width="100%">
<td>
<b><font size="2">Player<td>
<b><font size="2">Period<td>
<b><font size="2">Time<td>
<b><font size="2">Assist 1<td>
<b><font size="2">Assist 2<td>
<b><font size="2">SH<td>
<b><font size="2">PP<tr>
<td nowrap>
<font size="2">Oberpriller,&nbsp;D *<td>
<font size="2">3<td>
<font size="2">12:31<td nowrap>
<font size="2">Gronemeyer,&nbsp;S<td nowrap>
<font size="2">None<td>
<font size="2">
<center>
<td>
<font size="2">
<center>
</table>
<tr>
<td colspan="2">
<center>
<b><font size="+1">Penalties<tr>
<td valign="top">
<center>
<table border="1" cellspacing="0" width="100%">
<td>
<b><font size="2">Player<td>
<font size="2"><b>Period<td>
<font size="2"><b>Minutes<td>
<font size="2"><b>Offense<td>
<font size="2"><b>Start<td>
<font size="2"><b>Expired<tr>
<td nowrap>
<font size="2">Buschmann,&nbsp;C<td>
<font size="2">
<center>
3<td>
<font size="2">
<center>
2<td>
<font size="2">Interference<td>
<font size="2">11:11<td>
<font size="2">09:11<tr>
<td nowrap>
<font size="2">Buschmann,&nbsp;C<td>
<font size="2">
<center>
3<td>
<font size="2">
<center>
2<td>
<font size="2">Unsportmanlike Conduct<td>
<font size="2">03:26<td>
<font size="2">01:26<tr>
<td nowrap>
<font size="2">Bench<td>
<font size="2">
<center>
3<td>
<font size="2">
<center>
2<td>
<font size="2">Too Many Men<td>
<font size="2">01:46<td>
<font size="2">
00:00</table>
<td valign="top">
<center>
<table border="1" cellspacing="0" width="100%">
<td>
<b><font size="2">Player<td>
<font size="2"><b>Period<td>
<font size="2"><b>Minutes<td>
<font size="2"><b>Offense<td>
<font size="2"><b>Start<td>
<font size="2"><b>Expired<tr>
<td nowrap>
<font size="2">Marthaler,&nbsp;C<td>
<font size="2">
<center>
1<td>
<font size="2">
<center>
2<td>
<font size="2">Interference<td>
<font size="2">01:19<td>
<font size="2">16:19<tr>
<td nowrap>
<font size="2">Underwood,&nbsp;J<td>
<font size="2">
<center>
2<td>
<font size="2">
<center>
2<td>
<font size="2">Interference<td>
<font size="2">12:32<td>
<font size="2">10:32<tr>
<td nowrap>
<font size="2">Marthaler,&nbsp;C<td>
<font size="2">
<center>
3<td>
<font size="2">
<center>
2<td>
<font size="2">Interference<td>
<font size="2">11:39<td>
<font size="2">
09:39</table>
<tr>
<td colspan="2">
<center>
<font size="+1"><b>Goalies<tr>
<td>
<center>
<table border="1" cellspacing="0" width="100%">
<td>
<b><font size="2">Name<td>
<font size="2"><b>Shots<td>
<font size="2"><b>Goals<tr>
<td>
<font size="2">Baird,&nbsp;T<td>
<font size="2">20<td>
<font size="2">1<tr>
<td>
<font size="2"><b>Open Net<td>
<td>
<font size="2">
0</table>
<td>
<center>
<table border="1" cellspacing="0" width="100%">
<td>
<b><font size="2">Name<td>
<font size="2"><b>Shots<td>
<font size="2"><b>Goals<tr>
<td>
<font size="2">Hirdler,&nbsp;T<td>
<font size="2">42<td>
<font size="2">

Magically, all browsers seem to render this just fine. PHPTidy manages to do a good sense of it all, but the tables are nested so deeply and almost randomly that it's really hard to traverse this using DOM XPath.

Does anyone have any recommendations for other approaches for taking this on?

POST-MORTEM: After way too many Belgian wheat beers and dirtying up my code real good I got great results by removing all tags via strip_tags() except table, tr, and td, then running it through libtidy. It's now formatted beautifully and very easily traversed. Seems like it just needed a little massaging before sending it in to the parser.


Source: (StackOverflow)

Indenting by spaces using HTMLTidy in Notepad++

How do I make it so that instead of indenting my elements by multiples of 2 spaces (when formatting without wrapping is selected), HTMLTidy indents them by multiples of tabs (4 spaces long but only 1 byte in size)?


Source: (StackOverflow)

Install Tidy, PHP

I guess when php was installed on my box --with-tidy arg was not included.

When browsing my php.ini the line:

;extension=php_tidy.dll

is missing.

How can I add the module to my php install? Note I tried adding:

extension=php_tidy.dll

but with no luck.

I checked the docs but all they say is:

This extension is bundled with PHP 5 and greater, and is installed using the --with-tidy configure option.


Source: (StackOverflow)

How do I get HTML Tidy to not put newline before closing tags?

HTML Tidy has this infuriating habit of putting a newline before the closing tag. For example:

<p>Some text</p>

becomes

<p>Some text
</p>

How do I tell Tidy to keep the closing tag on the same line as the end of the content?

Btw, I am running Tidy through Notepad++, if that makes any difference.


Source: (StackOverflow)

Configuring and Using HTML Tidy

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 rel='nofollow' href="#">link</a>
</li>

Original Code (or What I need):

<li><a rel='nofollow' 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!


Source: (StackOverflow)

HTML tidy/cleaning in Ruby 1.9

I'm currently using the RubyTidy Ruby bindings for HTML tidy to make sure HTML I receive is well-formed. Currently this library is the only thing holding me back from getting a Rails application on Ruby 1.9. Are there any alternative libraries out there that will tidy up chunks of HTML on Ruby 1.9?


Source: (StackOverflow)

A command-line HTML pretty-printer: Making messy HTML readable

I'm looking for recommendations for HTML pretty printers which fulfill the following requirements:

  • Takes HTML as input, and then output a nicely formatted/correctly indented but "graphically equivalent" version of the given input HTML.
  • Must support command-line operation.
  • Must be open-source and run under Linux.

Source: (StackOverflow)

PHP Tidy removes whitespace and inserts newlines

I'd like to use PHP Tidy to ensure my xml is valid before I load it into a DomDocument.

However, I don't want Tidy to change something to my formatting - I only want it to repair problems like unbalanced tags, etc.

An example of the problem can be seen at this page: http://www.tek-tips.com/viewthread.cfm?qid=1654452

My own example is the following.

Input: <ex><context>собр<stress>а</stress>ние</context> акцион<stress>е</stress>ров — <stress>aa</stress>ndeelhoudersvergadering</ex> (which is valid xml already)

Expected output: <ex><context>собр<stress>а</stress>ние</context> акцион<stress>е</stress>ров — <stress>aa</stress>ndeelhoudersvergadering</ex> (there is breaking whitespace between </context> and актион)

Actual output:

<ex>
<context>собр
<stress>а</stress>ние</context>акцион
<stress>е</stress>ров — 
<stress>aa</stress>ndeelhoudersvergadering</ex>

(it removed the space between </context> and актион which will make the text unreadable, and it inserted newlines after each tag)

My code is:

function TidyXml($inputXml)
    {
        $config = array(
            'indent'         => false,
            'output-xml'     => true,
            'input-xml'     => true,
        );

        $tidy = new tidy();
        $tidy->parseString($inputXml, $config, 'utf8');
        $tidy->cleanRepair();
        $cleanXml = tidy_get_output($tidy);
        return $cleanXml;
    }

I tried changing several options, but didn't succeed.


Source: (StackOverflow)

Prevent PHP Tidy from converting style tag data to CDATA

I am using php tidy to clean a user generated HTML page which contains a style tag :

<style type="text/css">
    body {
        padding-top: 60px;
        padding-bottom: 40px;
    }
</style>

But once I run the Tidy, the style tag data is converted to CData. My main purpose of using Tidy is to repair the file as well as do proper indentation.

<style type="text/css">
/*<![CDATA[*/
    body {
            padding-top: 60px;
            padding-bottom: 40px;
    }
/*]]>*/
</style>

My Tidy config options are -

$options = array(
    'preserve-entities' => true,
    'hide-comments' => true,
    'tidy-mark' => false,
    'indent' => true,
    'indent-spaces' => 4,
    'new-blocklevel-tags' => 'article,header,footer,section,nav',
    'new-inline-tags' => 'video,audio,canvas,ruby,rt,rp',
    'doctype' => 'omit',
    'sort-attributes' => 'alpha',
    'vertical-space' => false,
    'output-xhtml' => true,
    'wrap' => 180,
    'wrap-attributes' => false,
    'break-before-br' => false,
    'vertical-space' => false,
);

$buffer = tidy_parse_string($buffer, $options, 'utf8');
tidy_clean_repair($buffer);

I tried searching a lot but the PHP Tidy library is not exactly a "well documented" one! So it came down to removing the CDATA manually after Tidy cleans/repairs the code.

$buffer = str_replace("/*<![CDATA[*/","",$buffer);
$buffer = str_replace("/*]]>*/","",$buffer);

Now the my problem with this approach is that the indentation of the style tag data is still screwed up (not exactly aligned with the rest of the page)

<style type="text/css">
    body {
        padding-top: 60px;
        padding-bottom: 40px;
    }
</style>

So again, how do I prevent TIDY from creating CDATA on the page!

Thanks a lot!


Source: (StackOverflow)

HTML Indentation in the mVc World [duplicate]

This question already has an answer here:

Here is a question that has been bugging me for a while, nowadays it's considered a good practice to indent HTML code but I've some reservations indenting code in a MVC pattern, here is a (silly) example:

HTML Code:

<!DOCTYPE html>
<html>
<head>
<title>Testing MVC Indentation</title>
</head>

<body>

<?php View('h1', 'I am a Level 1 Header'); ?>

    <table>
        <tr>
            <td>
                <?php View('h1', 'I am a Level 1 Header Inside a Table'); ?>
            </td>
        </tr>
    </table>

</body>
</html>

To indent correctly, the first call to the h1 view (or partial) should return:

\t<h1>I am a Level 1 Header</h1>

While the second call to the h1 view should return:

\t\t\t\t<h1>I am a Level 1 Header Inside a Table</h1>

The h1 view however, has no idea of the indentation scope it's in, so how the hell can it return the data properly indented? Also, ignoring indentation in views can disclose part of the application logic (check the HTML source code of this page after <div id="content"> for a real-world example):

<body>

<h1>I am a Level 1 Header</h1>

    <table>
        <tr>
            <td>
<h1>I am a Level 1 Header Inside a Table</h1>
            </td>
        </tr>
    </table>

</body>

Not indenting at all solves all problems, but it also makes it harder to read and maintain:

<body>

<h1>I am a Level 1 Header</h1>

<table>
<tr>
<td>
<h1>I am a Level 1 Header Inside a Table</h1>
</td>
</tr>
</table>

</body>

The only feasible solution I see to this problem is by using Tidy and output buffering, but I wonder if it's worth the effort since it will make processing and loading unnecessarily (?) slow. Also, this will not make it easier the maintain the HTML code since it only indents the output, not the source.

I'm sorry for the "basic" question, but I've been focusing on business logic for the last years and I've been kinda disconnected with the presentation world - in the good old days my HTML code was all unindented, but then again I also used tables to design the layout - just trying to catch up now.

Any solutions / insights on this subject?


Related Questions:


Source: (StackOverflow)

Tidy replacing   with a weird character

I am using Tidy (with PHP5) with UTF8 input, output, and char encoding enabled. When I clean a string with an &nbsp; in it, it is replacing it with an odd character. I've tried messing with the tidy config but nothing I try seems to work.

Before Tidy:

This is a test. &nbsp;Why does this not work?

After Tidy:

This is a test. ▒Why does this not work?

I don't know what the character is, but I assume it has something to do with the encoding of the enteties in utf8. Any ideas as to how I can get tidy to just leave the &nbsp; alone?


Source: (StackOverflow)