Hello

September 10, 2010, 10:48:56 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?
Login with username, password and session length
What sNews Is: - sNews is a simple, basic, customizable CMS tool suitable for developers with beginner-to-advanced PHP skills. It is also useful to have a good working knowledge of how to work with, set up and manage MySQL databases. sNews is not - geared towards the end-user who knows little or nothing about building and developing PHP-MySQL based websites.
News: Try sNews 1.7 DEMO
 
   Home   Help Search Login Register  
Pages: [1] 2
  Print  
Author Topic: [MOD]Auto <p> <br> </p> for editor  (Read 4256 times)
philmoz
High flyer
Administrator
ULTIMATE member
******

Karma: 152
Posts: 1848



WWW
« on: March 12, 2008, 10:59:27 AM »

This mod will convert single enter into <br />, double into a <p> ... </p> set.

It will store xhtml in the db for display in the browser.
In the editor, none of these tags will be returned for view, but will be stripped out.

Any extra markup (h1-6, span, div, etc) will not be stripped at editor view.

NOTE: the original is not my code (I'm not that clever). Please leave the credit statement in tact.

Functions affected.
processing()
form_articles()
js()

plus 2 additional functions.

processing()
Code:
locate
$text = clean($_POST['text']);

and make like

$text = clean(convertTags($_POST['text']));// converts various newlines (\n) to either <p> or <br />

form_articles()
Code:
in the text area for entry
this line
echo html_input('textarea', 'text', 'txt', $frm_text, l('text'), '', '', '', '', '', '2', '100', '', '', '');

to be

echo html_input('textarea', 'text', 'txt', remove_tags($frm_text), l('text'), '', '', '', '', '', '2', '100', '', '', '');

js() - replace the // generate preview function updatePreview() with
Code:
// generate preview
function updatePreview() {
if (document.getElementById('txt')) {
var body = document.getElementById('txt').value;
body = body.replace(/(\r\n\r\n|\r\r|\n\n)/g, "<p>");
body = body.replace(/(\r\n|\r|\n)/g, "<br />");
document.getElementById('preview').innerHTML = body;
}
}

2 new functions.
Code:
//Editor functions

// convertTags converts \n etc to either <p> or <br /> without
// breaking other html formatting, ready for writing to db.
function convertTags($pee, $br = 1) {// CODE COURTEOUSLY BY http://ma.tt/scripts/autop/
//locates newlines, and places in <p> ... </p> and <br /> tags
$pee = $pee . "\n"; // just to make things a little easier, pad the end
$pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
// Space things out a little
$allblocks = '(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|map|area|blockquote|address|math|style|input|p|h[1-6]|hr)';
$pee = preg_replace('!(<' . $allblocks . '[^>]*>)!', "\n$1", $pee);
$pee = preg_replace('!(</' . $allblocks . '>)!', "$1\n\n", $pee);
$pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
if ( strpos($pee, '<object') !== false ) {
     $pee = preg_replace('|\s*<param([^>]*)>\s*|', "<param$1>", $pee); // no pee inside object/embed
        $pee = preg_replace('|\s*</embed>\s*|', '</embed>', $pee);
    }
$pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
$pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "<p>$1</p>\n", $pee); // make paragraphs, including one at the end
$pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
$pee = preg_replace('!<p>([^<]+)\s*?(</(?:div|address|form)[^>]*>)!', "<p>$1</p>$2", $pee);
$pee = preg_replace( '|<p>|', "$1<p>", $pee );
$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
$pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
$pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
$pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
$pee = preg_replace('!<p>\s*(</?' . $allblocks . '[^>]*>)!', "$1", $pee);
$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*</p>!', "$1", $pee);
if ($br) {
     $pee = preg_replace_callback('/<(script|style).*?<\/\\1>/s', create_function('$matches', 'return str_replace("\n", "<WPPreserveNewline />", $matches[0]);'), $pee);
     $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
     $pee = str_replace('<WPPreserveNewline />', "\n", $pee);
}
$pee = preg_replace('!(</?' . $allblocks . '[^>]*>)\s*<br />!', "$1", $pee);
$pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)[^>]*>)!', '$1', $pee);
if (strpos($pee, '<pre') !== false)
    $pee = preg_replace_callback('!(<pre.*?>)(.*?)</pre>!is', 'clean_pre', $pee );
    $pee = preg_replace( "|\n</p>$|", '</p>', $pee );

    return $pee;

}

// remove_tags removes P and BR tags written to db, and replaces them with newline,
// for display in editor.
function remove_tags($text){//removes P and BR tags written to db, and replaces them.
$text = str_replace("<p>","",str_replace('<br />',"",str_replace("</p>","\r\n",str_replace('<p></p>',"",$text))));
return $text;
}
« Last Edit: March 12, 2008, 03:07:50 PM by philmoz » Logged

Take offs are optional, landings are mandatory, unless you get into space, then you have other issues.
henrich
Sr. Member
****

Karma: 23
Posts: 398


Passion for quality and excellence!


WWW
« Reply #1 on: March 12, 2008, 11:58:29 AM »

This mod will convert single enter into <br />, double into a <p> ... </p> set.
NOTE: the original is not my code (I'm not that clever). Please leave the credit statement in tact.

Can you explain a little bit more?
Logged

By(e) Henrich Smiley
------------------------------
My personal BLOG
centered
Guest
« Reply #2 on: March 12, 2008, 12:02:04 PM »

If you type an paragraph, instead of selecting the br or p tag, you can hit enter/return once and it will produce a br tag or hit enter/return twice to get p tags
Logged
Keyrocks
Doug
Administrator
ULTIMATE member
******

Karma: 420
Posts: 5573


Semantically Challenged


WWW
« Reply #3 on: March 12, 2008, 01:24:42 PM »

Quote from: philmoz
This mod will convert single enter into <br />, double into a <p> ... </p> set.
NOTE: the original is not my code (I'm not that clever). Please leave the credit statement in tact.
http://ma.tt/scripts/autop/


Automated Markup... a most useful mod. Would this be worth considering for 1.7?
Logged

Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU
funlw65
Hero Member
*****

Karma: 95
Posts: 799



WWW
« Reply #4 on: March 12, 2008, 11:48:02 PM »

I vote for inclusion Smiley . Is a must have. Faster typing. Also easy for non-coders.
Logged
Fred K (agentsmith)
sNews Dude
ULTIMATE member
*****

Karma: 104
Posts: 2338


da- dadadaaaa


« Reply #5 on: March 14, 2008, 02:14:12 PM »

I see stars. Five stars. One... two... three... 4, 5. Definitely one for the core.
Good on ya', mate!
Logged

Rui Mendes
Testing, Support
sNews Dude
Hero Member
*****

Karma: 180
Posts: 878


sNews1.7


WWW
« Reply #6 on: March 15, 2008, 11:16:10 AM »

Automated Markup... a most useful mod. Would this be worth considering for 1.7?

Definitely I VOTE YES.

Good one, thanks

Logged

Loretteville,Quebec@Canada.
centered
Guest
« Reply #7 on: March 17, 2008, 12:52:00 AM »

Phil - is the WPPreserveNewline needed inthis code?

Also would the p and br buttons still be needed?
« Last Edit: March 17, 2008, 01:25:51 AM by equilni » Logged
Sheepdisease
Jr. Member
**

Karma: -1
Posts: 79


« Reply #8 on: March 18, 2008, 01:50:57 AM »

This should be included in 1.7.
Logged
mattonik
Full Member
***

Karma: 6
Posts: 121



WWW
« Reply #9 on: March 18, 2008, 06:10:37 PM »

hi,great mod.
but i would like to ask, is there an option of excluding some parts from auto inserting?

for example i use the php code highliting mod and i don't want to add <br /> and <p> tags in between <code></code> tags. is it possible? thanks
Logged
invarbrass
Full Member
***

Karma: 17
Posts: 119


WWW
« Reply #10 on: March 22, 2008, 07:18:20 AM »

A useful mod indeed. Good work phil!
But there are situations where this feature could become a nuisance.... i.e. if the admin pastes HTML formatted inside the text box. At least this is what I'd do, instead of hand-coding evey bit of HTML tag, I'd write the content in DreamWeaver/NVU, and paste the appropriate source code into snews.
Logged
funlw65
Hero Member
*****

Karma: 95
Posts: 799



WWW
« Reply #11 on: March 22, 2008, 09:24:02 AM »

In these situations, a WYSIWYG editor have a switch mode, for editing raw html code ....

You cant ask too much from this mod. It needs the second part, (and we have it: original editor of sNews) and a mechanism for switching between them.
Logged
Keyrocks
Doug
Administrator
ULTIMATE member
******

Karma: 420
Posts: 5573


Semantically Challenged


WWW
« Reply #12 on: March 22, 2008, 03:02:51 PM »

A useful mod indeed. Good work phil!
But there are situations where this feature could become a nuisance.... i.e. if the admin pastes HTML formatted inside the text box. At least this is what I'd do, instead of hand-coding evey bit of HTML tag, I'd write the content in DreamWeaver/NVU, and paste the appropriate source code into snews.

Copying or cutting & pasting formatted HMTL from Dreamweaver or other similar applications is bound to create some conflicts with sNews formatting. When we make modifications to the existing sNews package, they are intended to work with it on the understanding that we don't insert pre-formatted HTML from Dreamweaver (or other). As funlw65 notes... "we can't ask too much from this mod... with the original editor of sNews".  Wink
Logged

Do it now... later may not come.
-------------------------------------------------------------------------------------------------
sNews 1.6 MESU | sNews 1.6 MEMU
philmoz
High flyer
Administrator
ULTIMATE member
******

Karma: 152
Posts: 1848



WWW
« Reply #13 on: March 25, 2008, 11:36:34 AM »

Essentially, this mod makes the <p> tags and <br /> tags invisible. They are still entered in the db, and still presented on browsing to the article.
This makes it possible to just write something without having to worry about your P's and Q's BR's

Copying the html from a dreamweaver authored article, and saving, then reloading in the editor, should only remove them from the editor window. All other formatting tags are kept in place and presented in the editor window.
Logged

Take offs are optional, landings are mandatory, unless you get into space, then you have other issues.
Armen
Sr. Member
****

Karma: 41
Posts: 334



WWW
« Reply #14 on: March 25, 2008, 11:57:00 AM »

Hey, hey, hey, slower there!

This feature should be DISABLEABLE if you actually decide to include it into the core.

I use html when editing an article and newlines are essential. I don't want snews to bound developer to no-newlines restriction!
Logged

Now ogres, oh, they're much worse. They'll make a suit from your freshly peeled skin. They'll shave your liver, squeeze the jelly from your eyes... Actually, it's quite good on toast.
Pages: [1] 2
  Print  
 
Jump to:  

English Steel 1.6 © Saxon North Technologies
Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!