Formatted code can be achieved with the help of some regular expressions. I was also frustrated with this issue. This is how I figured it out for myself. It is not perfect but as this is a simple script the result is quite good. Feel free to use it.
The $content is your content you actually posting. You need to insert this BEFORE it is saved. There are some "bugs" (I called them so) which are solved in a way that an extra space is inserted after a closing tag and before a newline. Not doing this way the code is sometimes (depending on browsers, too) collapsed back to one line. This workaround is not exactly what I wanted but could not figure out nothing better. Hope this help. I use this a year or so with relatively good output in FF.
- Code: Select all
$content = $_POST['content'];
$old = array (
'/(?<=<\/h\d>)(?=<)/', //01. headings
'/(?<=<\/div>)(?=<)/', //02. div
'/(?<=<\/p>)(?=<[^\/])/', //03. paragraph (/noscript exclusion)
'/(<li>[^<]+)?(<[uo]l>)(?=<li>)/', //04. nested or opening list (<li>text)?<uol><li>
'/(?<=<\/li>)(<\/[uo]l>)(?=<\/li>)/', //05. nested list </li></uol></li>
'/(?<=<\/li>)(<\/[uo]l>)(?!<\/li>)/', //06. list ending </li></uol>(not</li>)
'/(?<=<\/li>)(?=<li>)/', //07. li
'/(?<=<br \/>)(?=\S)/', //08. br
'/(?<!br)(?<= \/>)(?=\S)/', //09. img
'/(?<=script>)(?=<)/', //10. script/noscript
'/(?<=> \n)(?=<table)/', //11. table opening [with extra space bug]
'/(?<=>)(<caption>[^<]*)(?=<\/capt)/', //12. caption
'/(<tbody>)(<tr>)/', //13. tbody, tr [tbody is always added by wymeditor]
'/(?<=<\/td>)(?=<td>)/', //14. td
'/<\/tr>(<tr>)/', //15. tr
'/(<\/tr>)(<\/tbody>)(<\/table>)/' //16. table ending
);
$new = array (
" \n", //01. extra space
" \n", //02. extra space
" \n", //03. extra space
"$1\n $2\n ", //04.
"\n $1\n ", //05.
"\n $1 \n\n", //06.
"\n ", //07. 3 spaces (li after li)
"\n", //08. new textline after a br
" \n", //09. extra space
" \n", //10. extra space
"\n", //11.
" \n$1", //12.
"\n $1\n $2 \n ", //13.
"\n ", //14.
"\n </tr>\n $1 \n ", //15.
"\n $1\n $2 \n$3 \n\n" //16.
);
$content = preg_replace($old, $new, $content);