// Da HTML a testo leggibile, per i messaggi che sono solo HTML.
//
// Non si rende l'HTML (niente motore web, niente stili remoti): si tiene il
// testo, con i link ancora riconoscibili, così il linkificatore li rende
// toccabili. ES5 puro, provato con node (tests/test_htmltext.js).
// I blocchi che valgono un a capo.
var BLOCCHI = /<\/(p|div|tr|li|h[1-6]|blockquote|table|section|article)\s*>/gi;
var ACAPO = /
/gi;
// Le entità che si incontrano davvero; le numeriche si convertono dopo.
var ENTITA = {
"&": "&", "<": "<", ">": ">", """: "\"", "'": "'",
"'": "'", " ": " ", "…": "…", "—": "-",
"–": "-", "«": "«", "»": "»", "à": "à",
"è": "è", "é": "é", "ì": "ì", "ò": "ò",
"ù": "ù"
};
function decodifica(s) {
var out = s;
for (var k in ENTITA) {
if (ENTITA.hasOwnProperty(k)) {
out = out.split(k).join(ENTITA[k]);
}
}
// Numeriche: è e è
out = out.replace(/(\d+);/g, function (m, d) {
return String.fromCharCode(parseInt(d, 10));
});
out = out.replace(/([0-9a-fA-F]+);/g, function (m, h) {
return String.fromCharCode(parseInt(h, 16));
});
return out;
}
function daHtml(html) {
if (!html) return "";
var s = String(html);
// Via i blocchi che non sono testo.
s = s.replace(/