nl2br plugin by ophian

This commit is contained in:
Garvin Hicking 2011-11-24 12:21:40 +01:00
parent 2749dc54ad
commit 000f77224c
13 changed files with 301 additions and 152 deletions

View File

@ -4,6 +4,8 @@
Version 1.7 ()
------------------------------------------------------------------------
* Updated nl2br plugin (ophian)
* EXPERIMENTAL: Smarty3 support (ophian)
* Added new serendipity['defaultBaseURL'] variable that makes sure

View File

@ -1,8 +1,15 @@
<?php # $Id$
<?php # $Id: lang_de.inc.php 2492 2011-03-22 17:56:40Z ian $
@define('PLUGIN_EVENT_NL2BR_NAME', 'Textformatierung: NL2BR');
@define('PLUGIN_EVENT_NL2BR_DESC', 'Konvertiert Zeilenumbrüche zu HTML');
@define('PLUGIN_EVENT_NL2BR_ISOLATE_TAGS', 'Eine Liste von HTML-Tags, innerhalb derer keine Umbrüche konvertiert werden');
@define('PLUGIN_EVENT_NL2BR_CHECK_MARKUP', 'Überprüfe Markup-Plugins?');
@define('PLUGIN_EVENT_NL2BR_CHECK_MARKUP_DESC', 'Überprüft automatisch auf existierende Markup-Plugins, um die weitere Ausführung des NL2BR-Plugins zu untersagen. Dies gilt dann, wenn WYSIWYG oder spezifische Markup-Plugins entdeckt werden.');
@define('PLUGIN_EVENT_NL2BR_ISOLATE_TAGS', 'Eine Liste von HTML-Tags, innerhalb derer keine Umbrüche bei Benutzung von P-Tags konvertiert werden');
@define('PLUGIN_EVENT_NL2BR_ISOLATE_TAGS_DESC', 'Konfigurationsvorschlag: "code,pre,geshi,textarea". Trennen Sie mehrere HTML-Tags mit Komma. Hinweis: Die eingegebenen Tags sind reguläre Ausdrücke!');
@define('PLUGIN_EVENT_NL2BR_PTAGS', 'Nutze P-Tags');
@define('PLUGIN_EVENT_NL2BR_PTAGS_DESC', 'Setze statt br-Tags p-Tags ein.');
@define('PLUGIN_EVENT_NL2BR_ISOBR_TAG', 'ISOBR Isolations-Default BR Einstellung');
@define('PLUGIN_EVENT_NL2BR_ISOBR_TAG_DESC', 'Mit dem neu eingefügten NON-HTML-Tag <nl> </nl> als NL2BR Isolations-Default Einstellung, kann die NL2BR Funktion nun so genutzt werden, dass alles innerhalb dieses Tags nicht von NL2BR geparst wird. Auch nicht verschachtelte mehrfach Vorkommen im Text werden unterstützt! Beispiel: <nl>do not parse newline to br inside</nl>');
@define('PLUGIN_EVENT_NL2BR_CLEANTAGS', 'Nutze BR-Clean-Tags fallback, wenn ISOBR false');
@define('PLUGIN_EVENT_NL2BR_CLEANTAGS_DESC', 'Bei Benutzung von <HTML-Tags> in den Einträgen, die nicht zufriedenstellend mit der ISOBR Config-Option gelöst werden können, lösche nl2br Umbruch nach <tag>. Dies gilt für alle <tags>, die mit > oder >\n enden! Default (table|thead|tbody|tfoot|th|tr|td|caption|colgroup|col|ol|ul|li|dl|dt|dd)');
@define('PLUGIN_EVENT_NL2BR_CONFIG_ERROR', 'Konfigurations Fehler! Die Option: "clean_tags" wurde zurückgesetzt, weil die Option \'isobr\' aktiv geschaltet war! Benutzen sie bitte nur eine dieser Optionen.');

View File

@ -1,8 +1,15 @@
<?php # $Id$
<?php # $Id: lang_de.inc.php 2492 2011-03-23 17:57:00Z ian $
@define('PLUGIN_EVENT_NL2BR_NAME', 'Textformatierung: NL2BR');
@define('PLUGIN_EVENT_NL2BR_DESC', 'Konvertiert Zeilenumbrüche zu HTML');
@define('PLUGIN_EVENT_NL2BR_ISOLATE_TAGS', 'Eine Liste von HTML-Tags, innerhalb derer keine Umbrüche konvertiert werden');
@define('PLUGIN_EVENT_NL2BR_CHECK_MARKUP', 'Überprüfe Markup-Plugins?');
@define('PLUGIN_EVENT_NL2BR_CHECK_MARKUP_DESC', 'Überprüft automatisch auf existierende Markup-Plugins, um die weitere Ausführung des NL2BR-Plugins zu untersagen. Dies gilt dann, wenn WYSIWYG oder spezifische Markup-Plugins entdeckt werden.');
@define('PLUGIN_EVENT_NL2BR_ISOLATE_TAGS', 'Eine Liste von HTML-Tags, innerhalb derer keine Umbrüche bei Benutzung von P-Tags konvertiert werden');
@define('PLUGIN_EVENT_NL2BR_ISOLATE_TAGS_DESC', 'Konfigurationsvorschlag: "code,pre,geshi,textarea". Trennen Sie mehrere HTML-Tags mit Komma. Hinweis: Die eingegebenen Tags sind reguläre Ausdrücke!');
@define('PLUGIN_EVENT_NL2BR_PTAGS', 'Nutze P-Tags');
@define('PLUGIN_EVENT_NL2BR_PTAGS_DESC', 'Setze statt br-Tags p-Tags ein.');
@define('PLUGIN_EVENT_NL2BR_ISOBR_TAG', 'ISOBR Isolations-Default BR Einstellung');
@define('PLUGIN_EVENT_NL2BR_ISOBR_TAG_DESC', 'Mit dem neu eingefügten NON-HTML-Tag <nl> </nl> als NL2BR Isolations-Default Einstellung, kann die NL2BR Funktion nun so genutzt werden, dass alles innerhalb dieses Tags nicht von NL2BR geparst wird. Auch nicht verschachtelte mehrfach Vorkommen im Text werden unterstützt! Beispiel: <nl>do not parse newline to br inside</nl>');
@define('PLUGIN_EVENT_NL2BR_CLEANTAGS', 'Nutze BR-Clean-Tags fallback, wenn ISOBR false');
@define('PLUGIN_EVENT_NL2BR_CLEANTAGS_DESC', 'Bei Benutzung von <HTML-Tags> in den Einträgen, die nicht zufriedenstellend mit der ISOBR Config-Option gelöst werden können, lösche nl2br Umbruch nach <tag>. Dies gilt für alle <tags>, die mit > oder >\n enden! Default (table|thead|tbody|tfoot|th|tr|td|caption|colgroup|col|ol|ul|li|dl|dt|dd)');
@define('PLUGIN_EVENT_NL2BR_CONFIG_ERROR', 'Konfigurations Fehler! Die Option: "clean_tags" wurde zurückgesetzt, weil die Option \'isobr\' aktiv geschaltet war! Benutzen sie bitte nur eine dieser Optionen.');

View File

@ -1,15 +1,22 @@
<?php # $Id$
<?php # $Id: lang_en.inc.php 2492 2011-03-22 17:52:00Z ian $
/**
* @version $Revision$
* @version $Revision: 2491 $
* @author Translator Name <yourmail@example.com>
* EN-Revision: Revision of lang_en.inc.php
*/
@define('PLUGIN_EVENT_NL2BR_NAME', 'Markup: NL2BR');
@define('PLUGIN_EVENT_NL2BR_DESC', 'Convert newlines to BR tags');
@define('PLUGIN_EVENT_NL2BR_ISOLATE_TAGS', 'A list of HTML-tags where no breaks shall be converted');
@define('PLUGIN_EVENT_NL2BR_CHECK_MARKUP', 'Check other markup plugins?');
@define('PLUGIN_EVENT_NL2BR_CHECK_MARKUP_DESC', 'Automaticly check existing markup plugins to disable the use of NL2BR plugin. This is true, when WYSIWYG or specific markup plugins are detected.');
@define('PLUGIN_EVENT_NL2BR_ISOLATE_TAGS', 'A list of HTML-tags where no breaks shall be converted, if using P-Tags');
@define('PLUGIN_EVENT_NL2BR_ISOLATE_TAGS_DESC', 'Suggestion: "code,pre,geshi,textarea". Seperate multiple tags with a comma. Hint: The entered tags are evaluated as regular expressions.');
@define('PLUGIN_EVENT_NL2BR_PTAGS', 'Use P-Tags');
@define('PLUGIN_EVENT_NL2BR_PTAGS_DESC', 'Insert p-tags instead of br.');
@define('PLUGIN_EVENT_NL2BR_ISOBR_TAG', 'ISOBR isolations-default BR setting');
@define('PLUGIN_EVENT_NL2BR_ISOBR_TAG_DESC', 'With this newly added NON-HTML-Tag <nl> </nl>, as a NL2BR Isolations-Default setting, you can use the NL2BR function now by shutting down the parsing inside this tag. You can use it multiple times inside your entry, but not nested! Example: <nl>do not parse newline to br inside</nl>');
@define('PLUGIN_EVENT_NL2BR_CLEANTAGS', 'Use BR-Clean-Tags as fallback, when ISOBR false');
@define('PLUGIN_EVENT_NL2BR_CLEANTAGS_DESC', 'If using <HTML-Tags> in you entries, which can\'t be solved satisfiable with the ISOBR Config-Option, remove nl2br after <tag>. This applies to all <tags> ending with > or >\n! Default (table|thead|tbody|tfoot|th|tr|td|caption|colgroup|col|ol|ul|li|dl|dt|dd)');
@define('PLUGIN_EVENT_NL2BR_CONFIG_ERROR', 'Config missmatch alert! The Option: "clean_tags" is set back to false, while \'isobr\' is active! Just use one of them, please.');
?>

View File

@ -1,4 +1,4 @@
<?php # $Id$
<?php # $Id: serendipity_event_nl2br.php 2011-11-21 18:47:00Z ian $
@serendipity_plugin_api::load_language(dirname(__FILE__));
@ -14,14 +14,16 @@ class serendipity_event_nl2br extends serendipity_event
$propbag->add('description', PLUGIN_EVENT_NL2BR_DESC);
$propbag->add('stackable', false);
$propbag->add('author', 'Serendipity Team');
$propbag->add('version', '2.0');
$propbag->add('version', '2.12');
$propbag->add('requirements', array(
'serendipity' => '0.8',
'smarty' => '2.6.7',
'php' => '4.1.0'
));
$propbag->add('cachable_events', array('frontend_display' => true));
$propbag->add('event_hooks', array('frontend_display' => true,
'backend_display' => true,
'css' => true)
);
$propbag->add('groups', array('MARKUP'));
@ -45,13 +47,27 @@ class serendipity_event_nl2br extends serendipity_event
)
);
$conf_array = array('isolate', 'p_tags');
$conf_array = array('check_markup', 'isolate', 'p_tags', 'isobr', 'clean_tags');
foreach($this->markup_elements as $element) {
$conf_array[] = $element['name'];
}
$propbag->add('configuration', $conf_array);
}
function cleanup() {
global $serendipity;
/* check possible config mismatch setting */
if ( serendipity_db_bool($this->get_config('isobr')) === true && serendipity_db_bool($this->get_config('clean_tags')) === true ) {
$this->set_config('clean_tags', false);
echo '<div class="serendipityAdminMsgError"><img class="backend_attention" src="' . $serendipity['serendipityHTTPPath'] . 'templates/default/admin/img/admin_msg_note.png" alt="" />';
echo PLUGIN_EVENT_NL2BR_CONFIG_ERROR . '</div>';
return false;
}
return true;
}
function install() {
serendipity_plugin_api::hook_event('backend_cache_entries', $this->title);
}
@ -68,6 +84,13 @@ class serendipity_event_nl2br extends serendipity_event
function introspect_config_item($name, &$propbag)
{
switch($name) {
case 'check_markup':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_EVENT_NL2BR_CHECK_MARKUP);
$propbag->add('description', PLUGIN_EVENT_NL2BR_CHECK_MARKUP_DESC);
$propbag->add('default', 'true');
break;
case 'isolate':
$propbag->add('type', 'string');
$propbag->add('name', PLUGIN_EVENT_NL2BR_ISOLATE_TAGS);
@ -82,6 +105,20 @@ class serendipity_event_nl2br extends serendipity_event
$propbag->add('default', 'false');
break;
case 'isobr':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_EVENT_NL2BR_ISOBR_TAG);
$propbag->add('description', PLUGIN_EVENT_NL2BR_ISOBR_TAG_DESC);
$propbag->add('default', 'true');
break;
case 'clean_tags':
$propbag->add('type', 'boolean');
$propbag->add('name', PLUGIN_EVENT_NL2BR_CLEANTAGS);
$propbag->add('description', PLUGIN_EVENT_NL2BR_CLEANTAGS_DESC);
$propbag->add('default', 'false');
break;
default:
$propbag->add('type', 'boolean');
$propbag->add('name', constant($name));
@ -105,19 +142,55 @@ class serendipity_event_nl2br extends serendipity_event
function event_hook($event, &$bag, &$eventData) {
global $serendipity;
static $markup = null;
static $isolate = null;
static $p_tags = null;
static $isobr = null;
static $clean_tags = null;
global $_buf;
$hooks = &$bag->get('event_hooks');
if ($markup === null) {
$markup = serendipity_db_bool($this->get_config('check_markup'));
}
if ($p_tags === null) {
$p_tags = serendipity_db_bool($this->get_config('p_tags'));
}
if ($isobr === null) {
$isobr = serendipity_db_bool($this->get_config('isobr'));
}
if ($clean_tags === null) {
$clean_tags = serendipity_db_bool($this->get_config('clean_tags'));
}
if (isset($hooks[$event])) {
switch($event) {
case 'frontend_display':
// check single entry for temporary disabled markups
if ( !$eventData['properties']['ep_disable_markup_' . $this->instance] &&
!in_array($this->instance, (array)$serendipity['POST']['properties']['disable_markups']) &&
!$eventData['properties']['ep_no_textile'] && !isset($serendipity['POST']['properties']['ep_no_textile']) &&
!$eventData['properties']['ep_no_markdown'] && !isset($serendipity['POST']['properties']['ep_no_markdown'])) {
// yes, this markup shall be applied
$serendipity['nl2br']['entry_disabled_markup'] = false;
} else {
// no, do not apply markup
$serendipity['nl2br']['entry_disabled_markup'] = true;
}
// don't add additional br or p tags, if the wysiwyg-editor, the textile, or markdown plugin already took care about markup
if($markup) {
if ( ($serendipity['wysiwyg'] && serendipity_userLoggedIn()) ||
($serendipity['nl2br']['entry_disabled_markup'] === false && (class_exists('serendipity_event_textile') || class_exists('serendipity_event_markdown'))) ) {
return true;
}
}
// check for users isolation tags
if ($isolate === null) {
$isolate = $this->get_config('isolate');
$tags = (array)explode(',', $isolate);
@ -147,11 +220,38 @@ class serendipity_event_nl2br extends serendipity_event
$eventData[$element] = $this->isolate($eventData[$element], '~[<\[](' . implode('|', $isolate) . ').*?[>\]].*?[<\[]/\1[>\]]~si');
$eventData[$element] = nl2br($eventData[$element]);
$eventData[$element] = $this->restore($eventData[$element]);
} else {
if($isobr) {
$eventData[$element] = $this->isolate($eventData[$element], '~[<\[](nl).*?[>\]].*?[<\[]/\1[>\]]~si');
$eventData[$element] = nl2br($eventData[$element]);
$eventData[$element] = $this->restore($eventData[$element]);
// unset nl tagline, if is
$eventData[$element] = str_replace(array("<nl>", "</nl><br />", "</nl><br/>", "</nl>"), "", $eventData[$element]);
} else {
$eventData[$element] = nl2br($eventData[$element]);
}
}
/* this is an option if not using new isobr default config setting */
if (!$p_tags && $isobr === false && $clean_tags === true) {
// convert line endings to Unix style, if not already done
$eventData[$element] = str_replace(array("\r\n", "\r"), "\n", $eventData[$element]);
// clean special tags from nl2br
$eventData[$element] = $this->clean_nl2brtags($eventData[$element]);
}
}
}
return true;
break;
case 'backend_display':
/*
// create some buttons
if( $isobr ) {
// ToDo: hook into default/admin/entries.tpl somehow via the Heart Of Gold = serendipity_printEntryForm() before! it is loaded
$serendipity['smarty']->assign('iso2br', true);
}
*/
return true;
break;
@ -181,6 +281,32 @@ p.break {
}
}
/* clean nl2br from markup where it is invalid and/or breaks html output
* @param string entrytext
* @return string
* */
function clean_nl2brtags(&$entry) {
$allTags = explode('|', 'table|thead|tbody|tfoot|th|tr|td|caption|colgroup|col|ol|ul|li|dl|dt|dd');
$br2nl = array();
foreach($allTags as $tag){
/* for \\1 ( start with : < followed by any number of white spaces : \s* optionally a slash : /? and the tag itself )
* for \\2 ( anything with spaces and characters following until )
* for \\3 ( finally the > )
* for \\4 ( <br followed by any number of spaces, the optional slash and ending with > )
* regex modifier : i - using a case-insensitive match, as upper <TAGS> are valid in HTML
* regex modifier : s - using the dot metacharacter in the pattern to match all characters, including newlines */
$br2nl[] = "%(<\s*/?$tag)(.*?)([^>]*>)(<br\s*/?>)%is";
}
if(sizeof($br2nl)) $entry = preg_replace($br2nl, '\\1\\2\\3', $entry);
return $entry;
}
/* Insert <p class="whiteline" at paragraphs ending with two newlines
* Insert <p class="break" at paragraphs ending with one nl
* @param string text