Browse Source

fix: support des backticks simples, doubles et triples comme markdown. Le html produit est le même que pour les balises <code> a la seule différence qu'il est trimé (ce que fait markdown)

Refs: #4843
remotes/checkIfPRContentChanged-1664205915461169538/issue_4843
Cerdic 2 months ago
parent
commit
e8a8c80f5d
  1. 13
      inc/texte.php
  2. 14
      inc/textwheel.php
  3. 63
      wheels/spip/backticks.json
  4. 45
      wheels/spip/backticks.php
  5. 50
      wheels/spip/backticks.yaml

13
inc/texte.php

@ -829,6 +829,8 @@ function traiter_raccourcis($t, $show_autobr = false) {
* Texte transformé
**/
function propre($t, $connect = null, $env = []) {
static $wheel;
// les appels directs a cette fonction depuis le php de l'espace
// prive etant historiquement ecrits sans argment $connect
// on utilise la presence de celui-ci pour distinguer les cas
@ -848,6 +850,17 @@ function propre($t, $connect = null, $env = []) {
$t = pipeline('pre_echappe_html_propre', $t);
if (!isset($wheel)) {
$wheel = new TextWheel(
SPIPTextWheelRuleset::loader($GLOBALS['spip_wheels']['pre_echappe_html_propre'])
);
}
try {
$t = $wheel->text($t);
} catch (Exception $e) {
erreur_squelette($e->getMessage());
}
// Dans l'espace prive on se mefie de tout contenu dangereux
// avant echappement des balises <html>
// https://core.spip.net/issues/3371

14
inc/textwheel.php

@ -49,6 +49,20 @@ if (test_espace_prive()) {
$GLOBALS['spip_wheels']['raccourcis'][] = 'spip/ecrire';
}
// Si le tableau des pre_echappe_html_propre existe déjà
if (!isset($GLOBALS['spip_wheels']['pre_echappe_html_propre']) or !is_array($GLOBALS['spip_wheels']['pre_echappe_html_propre'])) {
$GLOBALS['spip_wheels']['pre_echappe_html_propre'] = [
'spip/backticks',
];
} else {
$GLOBALS['spip_wheels']['pre_echappe_html_propre'] = array_merge(
[
'spip/backticks',
],
$GLOBALS['spip_wheels']['pre_echappe_html_propre']
);
}
$GLOBALS['spip_wheels']['interdire_scripts'] = [
'spip/interdire-scripts'
];

63
wheels/spip/backticks.json

@ -0,0 +1,63 @@
{
"bt-echappe-tags": {
"match": ",</?[a-z!][^<>]*?[`][^<>]*>,imsS",
"if_chars": "<`",
"priority": -20,
"is_wheel": "Y",
"replace": {
"bt-echappe-tags-sub": {
"match": [
"`"
],
"replace": [
"\u0001\u0015"
],
"type": "str"
}
}
},
"bt-echappe-backticks": {
"if_str": "\\`",
"priority": -20,
"match": [
"\\\\",
"\\`"
],
"replace": [
"\u0001\u0010",
"\u0001\u0015"
],
"type": "str"
},
"bt-backticks-triples": {
"match": "@(?<!`)(```)([^`]|[^`].*?[^`])(```)($|[^`])@imsS",
"if_str": "```",
"is_callback": "Y",
"replace": "replace_backticks"
},
"bt-backticks-doubles": {
"match": "@(?<!`)(``)([^`]|[^`].*?[^`])(``)($|[^`])@imsS",
"if_str": "``",
"is_callback": "Y",
"replace": "replace_backticks"
},
"bt-backticks-simples": {
"match": "@(?<!`)(`)([^`]|[^`].*?[^`])(`)($|[^`])@imsS",
"if_str": "`",
"is_callback": "Y",
"replace": "replace_backticks"
},
"bt-restaure-tags": {
"if_chars": "\u0001",
"match": [
"\u0001\u0010",
"\u0001\u0015"
],
"replace": [
"\\",
"`"
],
"type": "str"
}
}

45
wheels/spip/backticks.php

@ -0,0 +1,45 @@
<?php
/**
* Fonctions utiles pour les wheels SPIP
*
* @SPIP\Textwheel\Wheel\SPIP\Fonctions
**/
if (!defined('_ECRIRE_INC_VERSION')) {
return;
}
include_spip('inc/texte');
function replace_backticks($match) {
$ticks = $match[1];
$code = $match[2];
$after = $match[4] ?? '';
// il ne faut pas passer dans entites_html, ne pas transformer les &#xxx; du code !
$code = spip_htmlspecialchars($code);
if (strlen($ticks) === 3) {
$code = nl2br($code);
// block code sur plusieurs lignes
$html = "<div style='text-align: left;' "
. "class='spip_code' dir='ltr'><code>"
. $code . '</code></div>';
$mode = "div";
}
else {
// span code
$html = "<code class='spip_code' dir='ltr'>" . trim($code) . '</code>';
$mode = "span";
}
$html = str_replace("\t", '&nbsp; &nbsp; &nbsp; &nbsp; ', $html);
$html = str_replace(' ', ' &nbsp;', $html);
$html = code_echappement($html, '', false, $mode);
return $html . $after;
}

50
wheels/spip/backticks.yaml

@ -0,0 +1,50 @@
--- # Prise en charge des backticks code
#
# Si on nomme une regle, elle devient surchargeable
bt-echappe-tags:
# voir bt-restaure-tags plus bas
match: ",</?[a-z!][^<>]*?[`][^<>]*>,imsS"
if_chars: "<`"
priority: -20
is_wheel: Y
replace:
bt-echappe-tags-sub:
match: ['`']
replace: ["\x1\x15"]
type: str
bt-echappe-backticks:
if_str: '\`'
priority: -20 # avant !paragraphes
match: ['\\', '\`']
replace: ["\x1\x10", "\x1\x15"]
type: str
bt-backticks-triples:
# raccourcis en ```..```
match: "@(?<!`)(```)([^`]|[^`].*?[^`])(```)($|[^`])@imsS"
replace: "replace_backticks"
if_str: '```'
type: str
bt-backticks-doubles:
# raccourcis en ``..``
match: "@(?<!`)(``)([^`]|[^`].*?[^`])(``)($|[^`])@imsS"
replace: "replace_backticks"
if_str: '``'
type: str
bt-backticks-simples:
# raccourcis en `..`
match: "@(?<!`)(`)([^`]|[^`].*?[^`])(`)($|[^`])@imsS"
replace: "replace_backticks"
if_str: '`'
type: str
bt-restaure-tags:
# voir echappe-tags ci-dessus
if_chars: "\x1"
match: ["\x1\x10", "\x1\x15"]
replace: ['\', '`']
type: str
Loading…
Cancel
Save