5.14.3 Getting started
5.14.3.1 Creating your first extension
Here we show how to create a very simple and mostly useless "hello world" extension, just to illustrate some basics. We will create a Cocoon pipeline which generates a blurb of HTML showing "Hello world" and then show how to include that in a Daisy document.
5.14.3.1.1 Create a directory for your extension
We will create a cross-site extension. To make it easy to add multiple extensions in a modular way, we'll organize the extensions in subdirectories. Therefore, edit or create a sitemap.xmap file at the following location:
<wikidata directory>/sites/cocoon/sitemap.xmap
For a site-specific extension, follow the same instructions but use <wikidata directory>/sites/<sitename>/cocoon as the base location in which you do everything.
and put the following in this sitemap.xmap (if there is already something in it, just replace it with this):
<?xml version="1.0"?>
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
<map:components>
</map:components>
<map:views>
</map:views>
<map:resources>
</map:resources>
<map:pipelines>
<map:pipeline>
<map:match pattern="*/**">
<map:act type="ResourceExists" src="{1}/sitemap.xmap">
<map:mount check-reload="yes" src="{../1}/sitemap.xmap" uri-prefix="{../1}"/>
</map:act>
</map:match>
</map:pipeline>
</map:pipelines>
</map:sitemap>
Then create a new subdirectory for your extension, lets say we call it mytest:
<wikidata directory>/sites/cocoon/mytest
With the sitemap.xmap file we just created, if you now request an extension URL starting with "mytest/", the cocoon/sitemap.xmap will try to forward the request processing to a sitemap located at cocoon/mytest/sitemap.xmap (this is what the map:mount does). We will create this sitemap in the next section.
5.14.3.1.2 Create a sitemap
In the above created directory (mytest), create a file called sitemap.xmap with the following content:
<?xml version="1.0"?>
<map:sitemap xmlns:map="http://apache.org/cocoon/sitemap/1.0">
<map:components>
</map:components>
<map:views>
</map:views>
<map:resources>
</map:resources>
<map:pipelines>
<map:pipeline type="noncaching">
<map:parameter name="outputBufferSize" value="8192"/>
<map:match pattern="hello">
<map:generate src="hello.xml"/>
<map:transform src="hello.xsl"/>
<map:serialize type="xml"/>
</map:match>
</map:pipeline>
</map:pipelines>
</map:sitemap>
The sitemap.xmap file is used by Cocoon to decide how to handle a request. This sitemap specifies that if the path matches "hello" (at least, the part of the path stripped from the part leading to this extension), then an XML-producing pipeline is executed which starts by reading and parsing the file hello.xml, transforming the parsed XML using the hello.xsl XSLT, and then finally serializing the result back to an XML document in the form of a byte stream (which is sent to the browser, or whoever made the HTTP request)
5.14.3.1.3 Create the file hello.xml
Still in the same directory, create a file called hello.xml with the following content:
<?xml version="1.0"?> <hello> <helloText>Hello world!</helloText> </hello>
5.14.3.1.4 Create the file hello.xsl
Also in the same directory, create a file called hello.xsl with the following content:
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="hello">
<div style="border: 1px solid blue;">
<xsl:value-of select="helloText"/>
</div>
</xsl:template>
</xsl:stylesheet>
For those unfamiliar with XSLT, this stylesheet defines a template which is executed when a hello element is encountered in the input. In that case, a div element is outputted with as content the value of the helloText element that is a child of the current hello element.
5.14.3.1.5 Try it out in your browser
Surf to the following URL (or equivalent), substituting <sitename> with the name of your Daisy Wiki site:
http://localhost:8888/daisy/<sitename>/ext/mytest/hello
If your browser shows XML (such as Firefox or IE), you will see this:
<div style="border: 1px solid blue;">Hello world!</div>
5.14.3.1.6 Include this in a Daisy document
To include this piece of HTML in a Daisy document:
- create or edit a document
- in the block-style dropdown, choose Include
- the paragraph switches to an include-style paragraph
- enter cocoon:/ext/mytest/hello for the content of the paragraph
Background detail: "cocoon:" URLs allow to make internal Cocoon requests, thus these requests don't go over HTTP, nor is the result of the called pipeline serialized in between (Cocoon pipelines are SAX-based, thus are not based on byte streams but rather XML-representing events).
- save the document, you should see the text "Hello world!" in a blue box in your document
5.14.3.2 Further pointers
You can now explore the various samples for further inspiration.
You'll see that many samples make use of Cocoon flowscript, which is basically Javascript with some Cocoon-specific APIs included (which also includes an advanced flow-control feature called continuations, but see the Cocoon documentation for that).
Daisy provides some additional functions for usage in flowscript, see the
In the sitemap, a line like this calls a function defined in a .js file:
<map:call function="minimalRss"/>
The function can then do some work (such as gathering data) and finally calls the sitemap again to show a page, using the sendPage function.
Many examples also make use of the Publisher, which is an
extension component running inside the repository server. Its purpose and
request format is described
Previous