Skip to content

Commit

Permalink
Item9235: Rename this thing, last time maybe...
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/TinyMCECleanStickyBitsPlugin@8121 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
PaulHarvey authored and PaulHarvey committed Jul 11, 2010
0 parents commit 80554d2
Show file tree
Hide file tree
Showing 12 changed files with 843 additions and 0 deletions.
40 changes: 40 additions & 0 deletions data/System/TinyMCECleanStickyBitsPlugin.txt
@@ -0,0 +1,40 @@
%META:TOPICPARENT{name="Plugins"}%
---+ !TinyMCE Clean Sticky Bits Plugin

%SHORTDESCRIPTION%

---++ Plugin Preferences
Provides the [[TinyMCEPlugin][TinyMCE]] plugin =foswikiclean=, which should be enabled by listing it in the [[TinyMCEPlugin#Settings][ =TINYMCEPLUGIN_ADDITIONAL_MCEPLUGINS= ]] preference setting. It provides the following toolbar buttons:

* =foswikiclean= - clean the current selection of any extraneous attributes that would cause the html2tml conversion to retain the HTML representation instead of being converted to WikiText. This is achieved in part by exporting the [[WysiwygPluginSettings][ =WYSIWYGPLUGIN_STICKYBITS= ]] preference variable to the =foswikiclean= plugin in !TinyMCE. The =foswikiclean= toolbar icon state changes to a clickable alert icon if the selection contains sticky attributes.

The TinyMCEPlugin toolbar may be customised to include these buttons by listing them in the [[TinyMCEPlugin#Settings][ =TINYMCEPLUGIN_ADDITIONAL_BUTTONS1=, =TINYMCEPLUGIN_ADDITIONAL_BUTTONS2= ]], etc. preference settings.

Alternatively, %TOPIC% will automatically:
* Add itself to the =TINYMCEPLUGIN_ADDITIONAL_PLUGINS= variable
* %ICON{"info"}% Inhibit this behaviour by setting =TINYMCECLEANSTICKYBITSPLUGIN_NO_AUTOLOAD = on=
* Insert the toolbar buttons to the right of the =tablecontrols= button, wherever that appears among the =TINYMCEPLUGIN_BUTTONS[n]= and =TINYMCEPLUGIN_ADDITIONAL_BUTTONS[n]= variables.
* %ICON{"info"}% Inhibit this behaviour by setting =TINYMCECLEANSTICKYBITSPLUGIN_NO_AUTOTOOLBAR = on=

<!--
One line description, required for extensions repository catalog.
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
-->

---++ Installation
%$INSTALL_INSTRUCTIONS%

---++ Plugin Info
This plugin uses icons from [[http://www.famfamfam.com/lab/icons/silk/]]
(see also: Foswiki:Extensions.FamFamFamContrib).

| Author: | Foswiki:Main.PaulHarvey - http://trin.org.au |
| Copyright: | &copy; 2010, Foswiki Contributors |
| License: | GPL ([[http://www.gnu.org/copyleft/gpl.html][GNU General Public License]]) |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change&nbsp;History: | <!-- versions below in reverse order -->&nbsp; |
| 11 Jul 2010 | Foswikitask:Item9235: Initial version |
| Home: | http://foswiki.org/Extensions/%TOPIC% |
| Support: | http://foswiki.org/Support/%TOPIC% |
| Development: | http://foswiki.org/Development/%TOPIC% |
82 changes: 82 additions & 0 deletions lib/Foswiki/Plugins/TinyMCECleanStickyBitsPlugin.pm
@@ -0,0 +1,82 @@
# See bottom of file for default license and copyright information

=begin TML
---+ package TinyMCECleanStickyBitsPlugin
=cut

package Foswiki::Plugins::TinyMCECleanStickyBitsPlugin;

# Always use strict to enforce variable scoping
use strict;
use warnings;

use Foswiki::Func (); # The plugins API
use Foswiki::Plugins (); # For the API version

our $VERSION = '$Rev$';
our $RELEASE = '11 Jul 2010';
our $SHORTDESCRIPTION =
'Toolbar button to clean a selection of sticky attributes that would prevent a clean conversion to WikiText';
our $NO_PREFS_IN_TOPIC = 1;

=begin TML
---++ initPlugin($topic, $web, $user) -> $boolean
=cut

sub initPlugin {
my ( $topic, $web, $user, $installWeb ) = @_;
my $init = 1;

if ( $Foswiki::Plugins::VERSION < 2.0 ) {
$init =
__PACKAGE__
. ' requires Foswiki::Plugins API >= 2.0, but '
. $Foswiki::Plugins::VERSION
. ' was detected';
}
elsif ( not $Foswiki::cfg{Plugins}{TinyMCEPlugin}{Enabled} ) {
$init = __PACKAGE__ . ' depends on TinyMCEPlugin, which is not enabled';
}
elsif ( not $Foswiki::cfg{Plugins}{JQueryPlugin}{Enabled} ) {
$init = __PACKAGE__ . ' depends on JQueryPlugin, which is not enabled';
}
elsif (
Foswiki::Func::getContext()->{'edit'}
and not(
Foswiki::Func::getPreferencesFlag(
'TINYMCEFOSWIKITOOLSPLUGIN_NO_AUTOTOOLBAR')
and Foswiki::Func::getPreferencesFlag(
'TINYMCEFOSWIKITOOLSPLUGIN_NO_AUTOLOAD')
)
)
{
# Lazy-load to help CGI compile times
require Foswiki::Plugins::TinyMCECleanStickyBitsPlugin::Core;
Foswiki::Plugins::TinyMCECleanStickyBitsPlugin::Core::setup();
}

return $init;
}

1;

__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2010 Paul.W.Harvey@csiro.au, http://trin.org.au
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.
211 changes: 211 additions & 0 deletions lib/Foswiki/Plugins/TinyMCECleanStickyBitsPlugin/Core.pm
@@ -0,0 +1,211 @@
# See bottom of file for default license and copyright information

=begin TML
---+ package TinyMCECleanStickyBitsPlugin::Core
=cut

package Foswiki::Plugins::TinyMCECleanStickyBitsPlugin::Core;

use strict;
use warnings;

use Foswiki::Func();

=begin TML
---++ setup()
=cut

sub setup {
my @plugin_ensure = qw(foswikiclean);
my @plugin_prefs = qw(MCEPLUGINS ADDITIONAL_MCEPLUGINS);
my $plugin_near = 'table';
my @buttons_ensure = qw(foswikiclean);
my @buttons_prefs = (
qw(BUTTONS1 BUTTONS2 BUTTONS3),
qw(ADDITIONAL_BUTTONS1 ADDITIONAL_BUTTONS2 ADDITIONAL_BUTTONS3)
);
my $buttons_near = 'tablecontrols';
my %stickybits = %{ _getWysiwygStickybits() };

if (
not Foswiki::Func::getPreferencesFlag(
'TINYMCECLEANSTICKYBITSPLUGIN_NO_AUTOLOAD')
)
{
_ensure( \@plugin_ensure, \@plugin_prefs, $plugin_near );
}
if (
not Foswiki::Func::getPreferencesFlag(
'TINYMCECLEANSTICKYBITSPLUGIN_NO_AUTOTOOLBAR')
)
{
_ensure( \@buttons_ensure, \@buttons_prefs, $buttons_near );
}
Foswiki::Func::addToZone( 'head', 'TinyMCECleanStickyBitsPlugin',
_genMeta( \%stickybits ) );

return;
}

sub _genMetaMarkup {
my ( $key, $value ) = @_;

return CGI::meta(
{
-name => 'foswiki.TinyMCECleanStickyBitsPlugin.' . $key,
-content => $value
}
) . "\n";
}

sub _genMeta {
my ($stickybits) = @_;
my $markup = '';
my $index = 0;

while ( my ( $tagname, $attributes ) = each %{$stickybits} ) {
my @attributePatterns = ();
my @attributeLiterals = ();
my $attributeLiteralString;
my $tagType;
$tagname =~ s/[\r\n\ ]//g;
if ( $tagname =~ /[^a-zA-Z0-9]/ ) {
$tagType = 'tagpattern';
}
else {
$tagType = 'tag';
}
$markup .= _genMetaMarkup( "$index.$tagType", $tagname );
foreach my $attribute ( @{$attributes} ) {
$attribute =~ s/[\r\n\ ]//g;
if ( $attribute =~ /[^a-zA-Z0-9]/ ) {
push( @attributePatterns, $attribute );
}
else {
push( @attributeLiterals, $attribute );
}
}
$attributeLiteralString = join( ', ', @attributeLiterals );
$markup .=
_genMetaMarkup( "$index.attributes", $attributeLiteralString );
my $attributePatternIndex = 0;
foreach my $attributePattern (@attributePatterns) {
$markup .=
_genMetaMarkup( "$index.attributePatterns.$attributePatternIndex",
$attributePattern );
$attributePatternIndex = $attributePatternIndex + 1;
}
if ( scalar(@attributePatterns) ) {
$markup .= _genMetaMarkup( "$index.attributePatterns.size",
$attributePatternIndex );
}
$index = $index + 1;
}
$markup .= _genMetaMarkup( 'size', $index );

return $markup;
}

# Ensure that items in $ensurelist appear in the values of $preflist,
# and if not, insert them into whatever preference contains $ensurenear.
# Magically, if the preferences aren't set, then pull them from topic sections
sub _ensure {
my ( $ensurelist, $preflist, $ensurenear ) = @_;
my %prefs = (); # preference values (keyed from $preflist)
my @absentlist = (); # $ensurelist items not appearing in $preflist values
my $target_pref; # The pref var to insert into

foreach my $pref ( @{$preflist} ) {
$prefs{$pref} = _getPref($pref);
}
$target_pref = _contains( $ensurenear, \%prefs );

# No point continuing if missing $near occurance to insert next to
if ($target_pref) {
my $setpref = $prefs{$target_pref};
foreach my $item ( @{$ensurelist} ) {
if ( not _contains( $item, \%prefs ) ) {
push( @absentlist, $item );
}
}
foreach my $item (@absentlist) {
$setpref =~ s/$ensurenear/$ensurenear, $item/;
}
Foswiki::Func::setPreferencesValue( 'TINYMCEPLUGIN_' . $target_pref,
$setpref );
}

return ( $target_pref and 1 );
}

sub _getWysiwygStickybits {
my $preftopic =
Foswiki::Func::getPreferencesValue('WYSIWYGPLUGIN_STICKYBITS')
|| $Foswiki::cfg{SystemWebName} . '.WysiwygPluginSettings';
my $preftext = Foswiki::Func::expandCommonVariables(<<"HERE");
%INCLUDE{"$preftopic" section="WYSIWYGPLUGIN_STICKYBITS" warn="off"}%
HERE
my %stickybits = ();

# Copied from Foswiki::Plugins::WysiwygPlugin::Handlers::protectedByAttr();
foreach my $def ( split( /;\s*/s, $preftext ) ) {
my ( $re, $attributes ) = split( /\s*=\s*/s, $def, 2 );
my @attributelist = split( /\s*,\s*/, $attributes );
$stickybits{ lc($re) } = \@attributelist;
}

return \%stickybits;
}

# Try to get a TinyMCEPlugin preference value; if not set, then try to get the
# default from TinyMCEPlugin's INIT_TOPIC via the equivalent topic section
sub _getPref {
my ($pref) = @_;
my $preftopic =
Foswiki::Func::getPreferencesValue('TINYMCEPLUGIN_INIT_TOPIC')
|| $Foswiki::cfg{SystemWebName} . '.TinyMCEPlugin';
my $value = Foswiki::Func::getPreferencesValue( 'TINYMCEPLUGIN_' . $pref )
|| Foswiki::Func::expandCommonVariables(<<"HERE");
%INCLUDE{"$preftopic" section="$pref" warn="off"}%\\
HERE

return $value;
}

# Check if \b$something\b is contained in a $list (hash). If it is, then return
# the key for the first occurance in which it was found.
sub _contains {
my ( $something, $list ) = @_;
my $found = 0;

while ( ( my ( $key, $value ) = each %{$list} ) and ( not $found ) ) {
if ( $value =~ /\b$something\b/ ) {
$found = $key;
}
}

return $found;
}

1;

__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2010 Paul.W.Harvey@csiro.au, http://trin.org.au
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.
2 changes: 2 additions & 0 deletions lib/Foswiki/Plugins/TinyMCECleanStickyBitsPlugin/DEPENDENCIES
@@ -0,0 +1,2 @@
Foswiki::Plugins::JQueryPlugin,>=6185,perl
Foswiki::Plugins::TinyMCEPlugin,>=8000,perl
12 changes: 12 additions & 0 deletions lib/Foswiki/Plugins/TinyMCECleanStickyBitsPlugin/MANIFEST
@@ -0,0 +1,12 @@
data/System/TinyMCECleanStickyBitsPlugin.txt 0644
lib/Foswiki/Plugins/TinyMCECleanStickyBitsPlugin.pm 0644
lib/Foswiki/Plugins/TinyMCECleanStickyBitsPlugin/Core.pm 0644
!noci
pub/System/TinyMCEPlugin/tinymce/jscripts/tiny_mce/plugins/foswikiclean/editor_plugin_src.js 0644
pub/System/TinyMCEPlugin/tinymce/jscripts/tiny_mce/plugins/foswikiclean/editor_plugin.js 0644
pub/System/TinyMCEPlugin/tinymce/jscripts/tiny_mce/plugins/foswikiclean/editor_plugin.js.gz 0644
pub/System/TinyMCEPlugin/tinymce/jscripts/tiny_mce/plugins/foswikiclean/langs/en.js 0644
pub/System/TinyMCEPlugin/tinymce/jscripts/tiny_mce/plugins/foswikiclean/langs/en.js.gz 0644
pub/System/TinyMCEPlugin/tinymce/jscripts/tiny_mce/plugins/foswikiclean/img/clean.png 0644
pub/System/TinyMCEPlugin/tinymce/jscripts/tiny_mce/plugins/foswikiclean/img/unclean.png 0644
pub/System/TinyMCEPlugin/tinymce/jscripts/tiny_mce/plugins/foswikiclean/img/readme.txt 0644

0 comments on commit 80554d2

Please sign in to comment.