source: spip-zone/_plugins_/pgn4spip/branches/pgn4spip_postpropre.php @ 90061

Last change on this file since 90061 was 90061, checked in by jack@…, 6 years ago

Avant de tout casser on passe le trunk dans "branches"

File size: 2.9 KB
RevLine 
[90061]1<?php
2/**********************************************************************************
3 * @Subject Fix _AUTOBR when the plugin is embedded in a table (see Test\SPIP3\Test08)
4 * @package pgn4spip plugin to embed pgn4web Chessboard in a SPIP 3.x article
5 * @version 2.61.0
6 * @copyright copyright (c) 2012 Matt Chesstale
7 * @license GNU General Public License version 3
8 * @compatible SPIP 3.x and SPIP 2.x
9 * @language PHP for SPIP 3 and SPIP 2
10 *
11 * @history:
12 * 2.61.0: Initial version for SPIP 3
13 * @reference: _AUTOBR in http://www.spip.net/en_article5533.html
14 * pipeline "post_propre" happens after the HTML iframe generation by pgn4spip_fonctions.php
15 * and after spip3\plugins-dist\textwheel\inc\texte.php replaces "\n" with "<p><br class='autobr' /></p>"
16 * The user does NOT need to define the constant _AUTOBR as '' in spip3\ecrire\mes_options.php
17 * @example
18        <?php
19        // Folder: ecrire\
20        define('_AUTOBR', ''); // Do not translate "\n" as "<p><br class='autobr' /></p>"
21        ?>
22 **********************************************************************************/
23if (!defined("_ECRIRE_INC_VERSION")) return; // No direct access allowed to this file
24
25define('AUTOBR_BEFORE', "</textarea>\n");
26define('CLS_AUTOBR', "<br class='autobr' />");
27define('AUTOBR_LINE', "<p>" . CLS_AUTOBR . "</p>\n");
28define('AUTOBR_AFTER', "<iframe src=");
29define('indFullPattern', 0);
30define('CLS_CHESSS', "class='chessboard-wrapper'"); // chessboard-wrapper class for the div
31
32// Fix AUTOBR in the generated HTML iframe and restore "{" "}" for a PGN comment
33// @in:         $flux the full article including the generated HTML iframe to run board.html
34// @return      $flux without AUTOBR between </textarea> and <iframe> inside a HTML table
35function pgn4spip_postpropre($flux)     
36{
37        if (strpos($flux, CLS_CHESSS))
38        {
39                $regex = "@<div.*" . CLS_CHESSS . ".*</div>@msU";
40                // http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
41                // m:PCRE_MULTILINE contains "\n"; s:PCRE_DOTALL including "\n"; + U:PCRE_UNGREEDY stop to first "</div>"
42                preg_match_all($regex, $flux, $matches, PREG_PATTERN_ORDER); // find all instances of generated plugin
43                $countPlugins = count($matches[0]); // Number of plugins
44                if ($countPlugins > 0) // iframe(s) found
45                {
46                        for ( $indPlugin=0; $indPlugin < $countPlugins; $indPlugin++ ) // For each iframe
47                        {       
48                                $strHtml = $matches[indFullPattern][$indPlugin];
49                                $strHtml = str_replace( AUTOBR_BEFORE . AUTOBR_LINE . AUTOBR_AFTER, 
50                                                                                AUTOBR_BEFORE . AUTOBR_AFTER, $strHtml); // Fix AUTOBR
51                                $strHtml = str_replace( CLS_AUTOBR, "", $strHtml); // Second pass without <p>...</p>
52                                $strHtml = str_replace("&#123;", "{", $strHtml); // Replace "&#123;PGN comment&#125;" with
53                                $strHtml = str_replace("&#125;", "}", $strHtml); // "{PGN comment}"
54                                $flux = str_replace($matches[indFullPattern][$indPlugin], $strHtml, $flux);
55                        }       
56                }               
57        }
58        return $flux;
59}
60?>
Note: See TracBrowser for help on using the repository browser.