%META:TOPICPARENT{name="Plugins"}%
---+!! Wysiwyg Plugin
<!--
   * Set SHORTDESCRIPTION = Translator framework for WYSIWYG editors
-->
<!-- Do _not_ attempt to edit this topic; it is auto-generated. Please add comments/questions/remarks to the Dev topic instead. -->
%SHORTDESCRIPTION%

Support for the integration of WYSIWYG (What-You-See-Is-What-You-Get) editors. On its own, the only thing this plugin gives you is a stand-alone HTML to TML (topic markup language) translator script. For WYSIWYG editing you will also need to install a specific editor package such as Foswiki:Extensions.TinyMCEPlugin.

This plugin provides a generic framework that supports editing of topics using any browser-based HTML editor. It works by transforming TML into HTML for the editor, and then transforming HTML back into TML on save.

%TOC%

---++ Features
   * Supports the input of malformed HTML
   * Full round-trip (TML -> XHTML -> TML)
   * Framework is editor-agnostic

---++ Details
---+++ What's in the package
The package includes the following pieces:
   * TML (topic markup language) to HTML translator
   * HTML to TML translator (with stand-alone script)
   * Generic plugin for automating the translation during editing

---+++ How it works
The plugin works by translating the topic text into HTML when someone edits a topic. The HTML is then fed to the WYSIWYG editor. On save, the edited HTML is run through the reverse translation before saving to the topic. TML is used in preference to HTML in the stored topic wherever possible, though HTML may be used if the translator can't find a suitable TML equivalent.

The default rendering that Foswiki uses to generate HTML for display in browsers is 'lossy' - information in the TML is lost in the HTML output, and a round-trip (recovering the original TML from the HTML) is impossible. To solve this problem the plugin instead uses its own translation of TML to XHTML. The generated XHTML is annotated with CSS classes that support the accurate recovery of the original TML.

_Before you ask the obvious question, yes, the translator *could* be used to replace the Foswiki rendering pipeline for generating HTML pages. In fact, the translator is taken almost directly from the implementation of the rendering pipeline for the TWiki-4 release_

Translation of the HTML back to TML uses the CPAN:HTML::Parser. This parser is used in preference to a more modern XML parser, because the WYSIWYG editor may not generate fully compliant XHTML. A strict parser would risk losing content. CPAN:HTML::Parser is better at handling malformed HTML.

There is also the advantage that the translator can be used to *import* HTML from other sources - for example, existing web pages. Due to the simple nature of TML and the potential complexity of web pages, this translation is often lossy - i.e. there will be HTML features that can be entered by editors that will be lost in this translation step. This is especially noticeable with HTML tables.

---+++ Using the translators from Perl scripts

Both translators can be used directly from Perl scripts, for example to build your own stand-alone translators.

A stand-alone convertor script for HTML to TML is included in the installation. It can be found in =tools/html2tml.pl=.

---+++ Integrating a HTML Editor
The plugin can be used to integrate an HTML editor in a number of different ways.
   1 The HTML for the content-to-be-edited can be generated directly in the standard edit template.
   1 The HTML for the content-to-be-edited can be generated directly in a specialised edit template.
   1 A URL can be used to fetch the content-to-be-edited from the server, for use in an IFRAME.
   1 REST handlers can be called from Javascript to convert content.

---++++ Generating content directly in the standard edit template
This is the technique used by WYSIWYG editors that can sit on top of HTML
textareas, such as !TinyMCE. The topic content is pre-converted to HTML before inclusion in the standard edit template. These editors use plugins that have a =beforeEditHandler= and an =afterEditHandler=. These handlers are responsible for the conversion of topic text to HTML, and post-conversion of HTML back to TML.
   1 User hits "edit".
   1 Editor-specific plugin =beforeEditHandler= converts topic content to HTML by calling =Foswiki::Plugins::WysiwygPlugin::TranslateTML2HTML=.
   1 User edits and saves
   1 Editor-specific plugin =afterEditHandler= converts HTML back to TML by calling =Foswiki::Plugins::WysiwygPlugin::TranslateHTML2TML=.
   * !WysiwygPlugin should *not* be enabled in =configure=.
   * =WYSIWYGPLUGIN_WYSIWYGSKIN= should *not* be set.
   * Your plugin should set the =textareas_hijacked= context id, to signal to skins to suppress their textarea manipulation functions.
This is the recommended integration technique, if your editor can support it.

---++++ Generating content directly in a specialised edit template
This technique is useful when the editor requires the topic content in a variety of different formats at the same time. In this scenario the editor uses a custom edit template. The WYSIWYG content is made available for instantiation in that template in a number of different formats. =WYSIWYGPLUGIN_WYSIWYGSKIN= *must* be set for this to work.

The flow of control is as follows:
   1 User hits "edit" with the skin (or cover) set the same as =WYSIWYGPLUGIN_WYSIWYGSKIN=.
   1 The !WysiwygPlugin =beforeEditHandler= determines if the topic is WYSIWYG editable, and vetos the edit if not by redirecting to the standard edit skin.
 the edit
   1 The =edit= template containing the JS editor is instantiated.
   1 The following macros are available for expansion in the template:
      * =%<nop>WYSIWYG_TEXT%= expands to the HTML of the content-to-be-edited. This is suitable for use in a =textarea=.
      * =%<nop>JAVASCRIPT_TEXT%= expands to the HTML of the content-to-be-edited in a javascript constant.
   1 User edits and saves
   1 The =afterEditHandler= in the !WyswiygPlugin sees that =wysiwyg_edit= is set, which triggers the conversion back to TML.

   * The HTML form in the edit template *must* include an =&lt;input= called =wysiwyg_edit= and set it to 1, to trigger the conversion from HTML back to TML.
   * =WYSIWYGPLUGIN_WYSIWYGSKIN= must be set to the name of the skin used for WYSIWYG editing. This is often the name of the editor e.g. =xinha=.

---++++ Fetching content from a URL
In this scenario, the edit template is generated *without* the content-to-be-edited. The content is retrieved from the server using a URL e.g. from an =IFRAME=.

The flow of control is as follows:
   1 As _Generating content directly in a specialised edit template_
   1 As _Generating content directly in a specialised edit template_
   1 As _Generating content directly in a specialised edit template_
   1 When the document loads in the browser, the JS editor invokes a content URL (using an =IFRAME= or a =XmlHttpRequest=) to obtain the HTML document to be edited
      * The content URL is just a Foswiki =view= URL with the =wysiwyg_edit=
        parameter set.
      * The !WysiwygPlugin recognises the =wysiwyg_edit= parameter and uses the
        TML2HTML translator to prepare the text, which is then returned
        as =text/plain= to the browser.
      * Two macros, =%<nop>OWEB%= and =%<nop>OTOPIC%=, can be used in the content URL in the edit template to refer to the source topic for the content.
   1 After edit handling is as for _Generating content directly in a specialised edit template_

---+++ Other techniques
---++++ Asynchronous saves
Editors can use =XmlHttpRequest= to perform saves, by POSTing to the Foswiki =save= script with the =wysiwyg_edit= parameter set to =1=. This parameter tells the =beforeSaveHandler= in the !WysiwygPlugin to convert the content back to TML. See %SYSTEMWEB%.CommandAndCGIScripts for details of the other parameters to the =save= script.

Once the save script has completed it responds with a redirect, either to an Oops page if the save failed, or to the appropriate post-save URL (usually a =view=). The editor must be ready to handle this redirect.

---++++ Handling Attachments
Attachment uploads can be handled by URL requests from the editor template to the Foswiki
=upload= script. The =upload= script normally redirects to the containing topic; a behaviour that you usually don't want in an editor! There are two ways to handle this:
   * If the uploads are done in an =IFRAME= or via =XmlHttpRequest=, then the
     302 redirect at the end of the upload can simply be ignored.
   * You can pass =noredirect= to the =upload= script to suppress the redirect. In this case
     you will get a =text/plain= response of =OK= followed by a message if everything went well, or an error message if it did not.

---++++ REST handlers
If you are confident in Javascript you can use REST handlers with =XmlHttpRequest= to convert content from TML to HTML and back again.

The plugin defines the following REST handlers:

=.../rest/WysiwygPlugin/html2tml?topic=Web.Topic;text=htmltexttotranslate=

Converts the HTML text to TML. =topic= *must* be specified.

=.../rest/WysiwygPlugin/tml2html?topic=Web.Topic;text=tmltexttotranslate=

Converts the TML text to HTML. =topic= *must* be specified. The response is a =text/plain= page of converted content.

---++ Plugin Installation Instructions
You do not need to install anything in the browser to use this extension. The following instructions are for the administrator who installs the extension on the server.

Open configure, and open the "Extensions" section. Use "Find More Extensions" to get a list of available extensions. Select "Install".

If you have any problems, or if the extension isn't available in =configure=, then you can still install manually from the command-line. See http://foswiki.org/Support/ManuallyInstallingExtensions for more help.


---++ Plugin Configuration Settings

---+++ Translator control
%INCLUDE{"%SYSTEMWEB%.WysiwygPluginSettings"}%

*Implementors note* if you are using your own before/after edit handlers, you can call =Foswiki::Plugins::WysiwygPlugin::isWysiwygEditable()= to check these controls.

---++ Known issues

---+++ Incompatible with "non-standard" syntax
WysiwygPlugin is incompatible with plugins that expand non-standard syntax e.g. Foswiki:Extensions.MathModePlugin (WysiwygPlugin)

Plugins that extend the syntax using macros, such as =%MYMACRO%=, should work fine.

*Implementors note* plugins that use XML-like tags may call [[%SCRIPTURLPATH{view}%/%SYSTEMWEB%/PerlDoc?module=Foswiki::Plugins::WysiwygPlugin][Foswiki::Plugins::WysiwygPlugin::addXMLTag()]] from their =initPlugin= handlers to make WysiwygPlugin protect the content between XML-like tags, just like it does for macros. 

---+++ Overlapping styles
Because Foswiki uses a "best guess" approach to some formatting, it allows overlapping of tags in a way forbidden by HTML, and it is impossible to guarantee 100% that formatting in the original Foswiki document will still be there when the same document is loaded and then saved through the WysiwygPlugin. The most obvious case of this is to do with styles. For example, the sentence
<verbatim class="tml">
*bold _bold-italic* italic_
</verbatim>
is legal in TML, but in HTML is represented by
<verbatim class="tml">
<strong>bold <em>bold-italic</em></strong> <em>italic</em>
</verbatim>
which gets translated back to TML as
<verbatim class="tml">
*bold _bold-italic_* _italic_
</verbatim>
which is correct by construction, but does not render correctly in Foswiki. This problem is unfortunately unavoidable due to the way TML works.

---+++ Rowspan processing needs %SYSTEMWEB%.TablePlugin

WysiwygPlugin is able to convert tables with cells that span rows into TML. This requires syntax provided by the %SYSTEMWEB%.TablePlugin (that is, the =| ^ |= markup). WysiwygPlugin will therefore only perform row-span related conversion if %SYSTEMWEB%.TablePlugin is enabled. %SYSTEMWEB%.TablePlugin is enabled by default and hence WysiwygPlugin converts tables with cells that span rows between TML and HTML by default.

If %SYSTEMWEB%.TablePlugin is *not* enabled, then TML table cells containing only =^= are not converted to rowspans, and HTML tables containing rowspans are not converted to TML.

---+++ TinyMCEPlugin integration
%STARTSECTION{"tinymceintegration"}%
   * Foswikitask:Item1396 - Anchors are not handled by WysiwygPlugin
   * Foswikitask:Item5955 - WysiwygPlugin fails to roundtrip tables with align="center", border attributes, etc.
      * Description: Sometimes tables will fail to be converted to TML syntax (will stay as HTML) because there are attributes on the table (such as alignment or border decorations) that WysiwygPlugin does not know how to preserve. If such attributes are necessary, please use [[VarTABLE]] instead.
      * Work-around:
         * Click inside the offending table
         * Click the table toolbar button (usually used to create a new table)
         * With the exception of =Cols= and =Rows=, delete/reset all content from the fields on the 'General' and 'Advanced' tabs.
         * Write a [[VarTABLE]] macro above the offending table that adds the desired attributes safely
%ENDSECTION{"tinymceintegration"}%

---++ Plugin Info

This plugin is brought to you by a [[http://wikiring.com][WikiRing <a href="http://wikiring.com"><img src="%ATTACHURLPATH%/wikiringlogo20x20.png" title="WikiRing: Professional Wiki Innovation and Support" /></a>]] partner - working together to improve your wiki experience!

Many thanks to the following sponsors for supporting this work:
   * [[http://ilog.fr][ILOG]]
   * [[http://www.carrier.com][Carrier Corporation]]
   * [[http://twiki.net][TWIKI.NET]]

|  Author: | [[http://c-dot.co.uk][Crawford Currie]], Foswiki Contributors |
|  Copyright | &copy; ILOG 2005 http://www.ilog.fr %BR% \
               &copy; 2008-2012 Foswiki Contributors |
|  License | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] |
|  Version: | 14548 (2012-04-04) |
|  Release: | 1.1.5 |
|  Change History: | |
|  1.1.5 (06 Apr 2012) | Foswikitask:Item11603: protect inline script and style tags from wysiwyg. %BR%\
                         Foswikitask:Item11440: protect tags inside pre.%BR%\
                         Foswikitask:Item9259: Protect TML tables from corrupting embedded html markup.%BR%\
                         Foswikitask:Item10125: Prevent #Anchors from being wrapped to the previous line.%BR%\
                         Foswikitask:Item11312: Prevent corruption of HTML tables containin blank lines. |
|  1.1.4               | Foswikitask:Item11378: support pass-through of DEL and INS tags |
|  1.1.3 (08 Nov 2011) | Foswikitask:Item2174: Fix WysiwygPlugin eating newlines inside =%MACRO{...}= expressions (Michael Tempest) |
|  1.1.2 (11 Apr 2011) | Version released with Foswiki 1.1.3. Only a minor change related to how the plugin is being upgraded |
|  1.1.1 (19 Jan 2011) | Foswikitask:Item10271: Switch to x.y.z release numbering %BR% \
                         Foswikitask:Item10048: Try to use Macros in the src URLs of images with titles %BR% \
                         Foswikitask:Item9973: Fix attachments REST handler to deal with topics named with international characters %BR%\
                         Foswikitask:Item1391: Protect div and span tags with =style= attributes |
|  28 Jun 2010 | Foswikitask:Item761, Foswikitask:Item2311, Foswikitask:Item5990, Foswikitask:Item9170: Fix conversion between character encodings. Any characters may be entered in the WYSIWYG editor, regardless of the site's encoding. Where possible, they are converted to the site encoding, otherwise they become entities. %BR%\
                 Foswikitask:Item2254: Fix cursor-movement problems on Mozilla browsers (introduced by Foswikitask:Item1798)%BR%\
                 Foswikitask:Item2605: Can now place cursor into empty list-item%BR%\
                 Foswikitask:Item1417: Can now move cursor above a table at start of a topic and below a table at the end of the topic %BR%\
                 Foswikitask:Item9148: Protect &lt;br /&gt; tags at the end of a protected line (e.g. in a macro parameter)%BR%\
                 Foswikitask:Item6068: Protect newlines within a &lt;pre&gt; block%BR%\
                 Foswikitask:Item2259: Keep the content of &lt;big&gt; and &lt;var&gt; tags %BR%\
                 Foswikitask:Item8289: Fix stand-alone (command-line) HTML-to-TML conversion |
|  21 May 2010 | Foswikitask:Item5221: Use Wysiwyg transition to remove usually unwanted paragraph html tags in table cells, which are otherwise impossible to remove in !TinyMCE up to at least 3.3.6%BR%\
                 Foswikitask:Item8274: Fix problem where Wysiwyg transition merges two consecutive lists (a result of work on Foswikitask:Item2254) |
|  17 Jan 2010 | Foswikitask:Item2337: ATTACHFILESIZELIMIT check fails confusingly if value is "0 " |
|  18 Dec 2009 | Foswikitask:Item2511: move code out of the plugin module to accelerate loading |
|  18 Nov 2009 | Foswikitask:Item2369: Convert tables with cells that span rows |
|  22 Oct 2009 | Foswikitask:Item2183: Protect div style= by default |
|  18 Sep 2009 | Foswikitask:Item1980: Prevent dataloss when saving a topic in Wysiwyg where there are a pair of sticky tags inside verbatim tags |
|  28 Jun 2009 | Foswikitask:Item1770: Protect XML tags registered by plugins, and not just the content between them (Michael Tempest) |
|  06 Jun 2009 | Foswikitask:Item1013: Correct dependency on HTML::Parser (Will Norris) Foswikitask:Item1397: Foswikitask:Item1535: Foswikitask:Item1666: Correct processing of colour and typewriter-text in several situations, include application to bold text and table cells (Michael Tempest) Foswikitask:Item1667: Remove unwanted extra &lt;sticky&gt; tags (Michael Tempest) Foswikitask:Item1674: Let plugins register XML tags that should be protected like macros |
|  10 Apr 2009 | Foswikitask:Item1394: fixed colour handling |
|  03 Dec 2008 | Foswikitask:Item6041: fixed empty bullet list problem. Foswiki version |
|  22 Oct 2008 | Fixed TWikibug:Item5961 (emphasis), TWikibug:Item6089 (backslash in verbatim) |
|  07 Aug 2008 | Fixed TWikibug:Item5707 (mod_perl) |
| See Subversion logs for earlier versions ||
|  Dependencies: | <table class="foswikiTable" border="1"><tr><th>Name</th><th>Version</th><th>Description</th></tr><tr><td align="left">HTML::Parser</td><td align="left">&gt;=3.28</td><td align="left">Required. Available from [[http://cpan.uwinnipeg.ca/search?query=HTML%3A%3AParser&mode=dist][CPAN]].</td></tr><tr><td align="left">HTML::Entities</td><td align="left">&gt;=1.25</td><td align="left">Required. Available from [[http://cpan.uwinnipeg.ca/search?query=HTML%3A%3AEntities&mode=dist][CPAN]].</td></tr><tr><td align="left">Encode</td><td align="left">&gt;=2.01</td><td align="left">Required.</td></tr></table> |
|  Plugin Home: | http://foswiki.org/Extensions/%TOPIC% |
|  Support: | http://foswiki.org/Support/%TOPIC% |

<!-- Do _not_ attempt to edit this topic; it is auto-generated. Please add comments/questions/remarks to the Dev topic instead. -->

%META:FILEATTACHMENT{name="wikiringlogo20x20.png" attr="h" comment="" version="1"}%
