developer.jelix.org is not used any more and exists only for history. Post new tickets on the Github account.
developer.jelix.org n'est plus utilisée, et existe uniquement pour son historique. Postez les nouveaux tickets sur le compte github.

Opened 12 years ago

Closed 11 years ago

Last modified 10 years ago

#760 closed new feature (fixed)

New modifier for tpl/common : truncatehtml

Reported by: isepman Owned by: isepman
Priority: low Milestone: Jelix 1.2 beta
Component: jelix:plugins:tpl Version: trunk
Severity: trivial Keywords: tpl modifier truncate
Cc: Blocked By:
Blocking: Documentation needed: no
Hosting Provider: Php version: 5.2

Description

This patch provide a new function for jTpl: truncatehtml this function allows to truncate a html formatted string while closing previously opened tags

Attachments (6)

truncatehtml.diff (2.3 KB) - added by isepman 12 years ago.
patch to apply to install the function
truncatehtml.2.diff (2.5 KB) - added by isepman 12 years ago.
Patch with unit tests
truncatehtml+test.diff (5.8 KB) - added by isepman 12 years ago.
real patch with tests
truncatehtml+test-vrai.diff (5.3 KB) - added by isepman 12 years ago.
le VRAI fichier avec les débuts de test (est-ce possible de supprimer les autres?)
truncatehtml+test_3.diff (6.4 KB) - added by isepman 12 years ago.
Fonction + 11 tests passés
truncatehtml+test_4.diff (6.9 KB) - added by isepman 12 years ago.
Dernier test, attribut des balises non auto fermantes

Download all attachments as: .zip

Change History (27)

Changed 12 years ago by isepman

patch to apply to install the function

comment:1 Changed 12 years ago by isepman

  • Keywords modifier added; function plugin removed
  • Summary changed from New function for tpl/common : truncathtml to New modifier for tpl/common : truncatehtml
  • Type changed from bug to new feature

comment:2 Changed 12 years ago by isepman

  • Resolution set to fixed
  • Status changed from new to closed

comment:3 Changed 12 years ago by isepman

  • Resolution fixed deleted
  • Status changed from closed to reopened

comment:4 follow-up: Changed 12 years ago by bastnic

Salut,

J'ai déjà eu le même besoin et j'avais codé ça à partir d'une classe cakephp : http://forge.jelix.org/projects/wechange/browser/app/plugins/tpl/common/modifier.truncatehtml.php

A mon avis ma version est assez crade.

A voir / a tester laquelle fonctionne le mieux. Peux-tu fournir des tests unitaires stp ?

comment:5 Changed 12 years ago by laurentj

  • Milestone set to Jelix 1.2

comment:6 in reply to: ↑ 4 Changed 12 years ago by isepman

Replying to bastnic: test commencé, je ne savais pas trop comment m'y prendre pour tester un modifier, mais voila, je poste. Attention, le modifier ne passait pas les test ! du coup, j'ai corrigé quelques trucs.

J'lest mets en vrac (.diff)

Changed 12 years ago by isepman

Patch with unit tests

Changed 12 years ago by isepman

real patch with tests

Changed 12 years ago by isepman

le VRAI fichier avec les débuts de test (est-ce possible de supprimer les autres?)

comment:7 follow-up: Changed 12 years ago by bastnic

Non impossible de supprimer un patch déjà attaché (du moins sans être Dieu l'admin ;)). Et présentement ça ne sert à rien.

Que donnent tes tests ? niveau qualité de résultats et perf ?

comment:8 in reply to: ↑ 7 Changed 12 years ago by isepman

Ba écoute, mes tests ont l'air de marcher, je ne sais pas si t'as regardé mais pour ma fonction en tout cas, tous les tests que j'ai faits sont passé:

testé : couper au milieu d'un mot (ou d'une entité xml),

testé : couper au milieu d'une balise html (<img src=""... interdit!),

testé : ne pas rajouter de tag fermant sur une balise auto-fermante, (genre <br />...</br>),

testé : ne pas inclure les commentaires HTML,

testé : couper a 0,

testé : couper trop loin (ne pas ajouter le ...),

testé : ajouter autre chose que le ... a la fin,

testé : mettre une balise commentaire sans la fermer. (ca peut arriver quand sql nous renvoie une chaine tronquée),

testé : le cas ou tout vas bien (quand même!)


Attention, c'est assez compliqué: le nombre de lettres qu'on indique comprend la longueur des balises html ainsi que des entités xml.

Attention 2, si on met un truc du genre <strong>test</strong> il sera évalué comme un mot de 21 lettres et si on coupe au milieu, ce mot ne sera pas inclus dans la chaine résultante. C'est pas un peu restrictif quand meme?

suggestion : doit on interdire les balises <script><object><embed><frame><iframe>? Ca me semblerais judicieux (sutrout que leur contenu est souvent très long), mais du coup la fonction ne fait plus que tronquer du html. A voir...

J'ai encore du modifier des trucs je poste donc truncatehtml+test_3.diff qui contient ces modifs (en espérant que ca marche du premier coup...)

Changed 12 years ago by isepman

Fonction + 11 tests passés

comment:9 Changed 12 years ago by isepman

nouveau test passé : le plugin ne reconnaissait pas les attributs des tags (il pensait que <div class="class"> est une balise autofermante..., ce bug n'était pas la a début, mais avec mes modifs...), bref, c'est corrigé, les tests passent, et ca marche à l'utilisation...

Changed 12 years ago by isepman

Dernier test, attribut des balises non auto fermantes

comment:10 Changed 11 years ago by foxmask

i dunno if it's still usefull but if yes ; it miss to set review to "Review?"

comment:11 Changed 11 years ago by bballizlife

isepman, do you need review on your last patch or do you have to work again on it ? If you need review, thanks to set the review property on "review?"

comment:12 Changed 11 years ago by isepman

  • review set to review+

Ooops, My bad, I think my new stuff works, it's tested, and I've been using it in production for two months, without any problem (yet). So I'm submitting it for review... Sorry for the delay, I'm not working on it anymore...

comment:13 Changed 11 years ago by isepman

  • review changed from review+ to review?

comment:14 Changed 11 years ago by isepman

Sorry again, I'm explaining it in english right now : This function is to truncate HTMLtext without messing the whole page with non closed tags... With this modifier, you can actually truncate a HTML and close the previously opened tags... Nevertheless, there are still some issues I raised before (in french):

When you say "{truncatehtml $sentence, 300}", the HTML tags included in the 300 you put are counted in the truncature : "<li>A</li>" is 10 characters.

The javascripts, embed, iframe and stuff like that are simply removed (if you're truncating, you don't want to display fancy things, you only want to summarize... That's what I think..)

But In the end, I think we have a solid function, and as I said (in french) I've been using this modifier in production without problem...

comment:15 Changed 11 years ago by bballizlife

Could it be possible to only count the characters that are displayed or do you think it would be too complex to implement ?

comment:16 Changed 11 years ago by geekbay

when i have tested this modifier, i got this warning:

[notice 8] Undefined variable: openedtags /var/www/site/plugins/tpl/common/modifier.truncatehtml.php 52

we just need to add $openedtags = array(); somewhere before the foreach

comment:17 Changed 11 years ago by laurentj

  • Component changed from jelix:plugins to jelix:plugins:tpl
  • review changed from review? to review+

ok for me, if this notice is fixed too.

comment:18 Changed 11 years ago by laurentj

  • Resolution set to fixed
  • Status changed from reopened to closed

landed in the trunk. r1456.

comment:19 Changed 11 years ago by laurentj

Thanks for your patch.

comment:20 Changed 10 years ago by laurentj

  • Documentation needed set
Note: See TracTickets for help on using tickets.