This Howto page describes how to add a new visitor over a document. The creation of a statistic component is depicted step by step. The idea is to have displayed the number of children if we are viewing a folder, or the size of a page if one is visited.
3 new classes and 1 class extension are needed:
TemplateStatistic>>accept: aVisitor aVisitor acceptTemplateStatistic: self
VisitorRendererHtmlLight>>acceptTemplateStatistic: aTemplate | visitor | visitor := VisitorStatistic on: stream. action structure accept: visitor
VisitorStatistic class>>on: anHtmlStream ^ self new htmlStream: anHtmlStream; yourself
VisitorStatistic>>acceptChapter: structure htmlStream class: 'boxtitle'. htmlStream div: \[htmlStream text: 'Folder Statistic']. htmlStream class: 'boxcontent'. htmlStream div: \[htmlStream italic: \[htmlStream text: structure title , ' has ']. htmlStream bold: \[htmlStream text: structure size printString]. htmlStream italic: \[htmlStream text: ' children']]. htmlStream break
This code display a purple box titled ’Folder Statistic’ displaying the number of chidren.
VisitorStatistic>>acceptPage: structure htmlStream class: 'boxtitle'. htmlStream div: \[htmlStream text: 'Page Statistic']. htmlStream class: 'boxcontent'. htmlStream div: \[htmlStream italic: \[htmlStream text: 'Size of ', structure title , ' is ']. htmlStream bold: \[htmlStream text: (self sizeOfDocument: structure document) printString]]. htmlStream break
The compiler should complain about the message sizeOfDocument: because it is implemented nowhere.
VisitorStatistic>>sizeOfDocument: aDocument VisitorStatistic>> | visitor | VisitorStatistic>> visitor := VisitorSize new. VisitorStatistic>> aDocument accept: visitor. VisitorStatistic>> ^ visitor size
VisitorSize>>initialize super initialize. size := 0
VisitorSize>>size ^ size
VisitorSize>>acceptText: aText size := size + aText text size
VisitorRendererHtmlLight class>>defaultTemplate ... add: (TemplateStructuresReferences new); add: (TemplateStatistic new); "<== The new line !!" yourself); ...
server root template: VisitorRendererHtmlLight defaultTemplate.
In this howto we have learned how to create a new template. As remainder a hierarchy of template is used for describing the visual aspect of the wiki. We want to add a new component (a purple box), so we add a new Template.
In order to be displayed in a web browser, a visitor rendered is run over the whole template structure and on the page or the folder displayed. This visitor need to be extended (or a new can be redefined using subclassing) in order to display the new component.
Our new component need to know the textual size of a page, we create a new visitor charged to compute the number of character a page contains.
That’s all folk! Enjoy!