Is there an option to keep the whitespace in the source ... it definitely helps the code readability when working directly with the source.
Toby Hede
-----------------------------------
www.finitestatemachine.com
Forum closed. New forum available at http://community.wymeditor.org/
return this.parser.parse(this.html());
return this.html
jfh wrote:The parsing is taking place in the XhtmlParser, and is used by the xhtml() function, in jquery.wymeditor.js:
- Code: Select all
return this.parser.parse(this.html());
You can for example override that function, returning a more readable source.
var newHtml = wymEditor.xhtml();
newHtml = newHtml
.replace(/<\/h(\d)>/g, "</h$1>\r\n\r\n")
.replace(/<\/div>/g, "</div>\r\n\r\n")
.replace(/<\/p>/g, "</p>\r\n\r\n");
//Function to seperate multiple tags one line
function fix_newlines_for_clean_html($fixthistext)
{
$fixthistext_array = explode("\n", $fixthistext);
foreach ($fixthistext_array as $unfixedtextkey => $unfixedtextvalue)
{
//Makes sure empty lines are ignores
if (!preg_match("/^(\s)*$/", $unfixedtextvalue))
{
$fixedtextvalue = preg_replace("/>(\s|\t)*</U", ">\n<", $unfixedtextvalue);
$fixedtext_array[$unfixedtextkey] = $fixedtextvalue;
}
}
return implode("\n", $fixedtext_array);
}
function clean_html_code($uncleanhtml)
{
//Set wanted indentation
$indent = " ";
//Uses previous function to seperate tags
$fixed_uncleanhtml = fix_newlines_for_clean_html($uncleanhtml);
$uncleanhtml_array = explode("\n", $fixed_uncleanhtml);
//Sets no indentation
$indentlevel = 0;
foreach ($uncleanhtml_array as $uncleanhtml_key => $currentuncleanhtml)
{
//Removes all indentation
$currentuncleanhtml = preg_replace("/\t+/", "", $currentuncleanhtml);
$currentuncleanhtml = preg_replace("/^\s+/", "", $currentuncleanhtml);
$replaceindent = "";
//Sets the indentation from current indentlevel
for ($o = 0; $o < $indentlevel; $o++)
{
$replaceindent .= $indent;
}
//If self-closing tag, simply apply indent
if (preg_match("/<(.+)\/>/", $currentuncleanhtml))
{
$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
}
//If doctype declaration, simply apply indent
else if (preg_match("/<!(.*)>/", $currentuncleanhtml))
{
$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
}
//If opening AND closing tag on same line, simply apply indent
else if (preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && preg_match("/<\/(.*)>/", $currentuncleanhtml))
{
$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
}
//If closing HTML tag or closing JavaScript clams, decrease indentation and then apply the new level
else if (preg_match("/<\/(.*)>/", $currentuncleanhtml) || preg_match("/^(\s|\t)*\}{1}(\s|\t)*$/", $currentuncleanhtml))
{
$indentlevel--;
$replaceindent = "";
for ($o = 0; $o < $indentlevel; $o++)
{
$replaceindent .= $indent;
}
$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
}
//If opening HTML tag AND not a stand-alone tag, or opening JavaScript clams, increase indentation and then apply new level
else if ((preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && !preg_match("/<(link|meta|base|br|img|hr)(.*)>/", $currentuncleanhtml)) || preg_match("/^(\s|\t)*\{{1}(\s|\t)*$/", $currentuncleanhtml))
{
$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
$indentlevel++;
$replaceindent = "";
for ($o = 0; $o < $indentlevel; $o++)
{
$replaceindent .= $indent;
}
}
else
//Else, only apply indentation
{$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;}
}
//Return single string seperated by newline
return implode("\n", $cleanhtml_array);
}
Users browsing this forum: No registered users and 3 guests