Skip to content

Instantly share code, notes, and snippets.

@kurokikaze
Created January 19, 2011 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurokikaze/786032 to your computer and use it in GitHub Desktop.
Save kurokikaze/786032 to your computer and use it in GitHub Desktop.
Система верстки на XML+XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common"
xmlns:math="http://exslt.org/math"
xmlns:date="http://exslt.org/dates-and-times"
xmlns:func="http://exslt.org/functions"
xmlns:set="http://exslt.org/sets"
xmlns:str="http://exslt.org/strings"
xmlns:dyn="http://exslt.org/dynamic"
xmlns:saxon="http://icl.com/saxon"
xmlns:xalanredirect="org.apache.xalan.xslt.extensions.Redirect"
xmlns:xt="http://www.jclark.com/xt"
xmlns:libxslt="http://xmlsoft.org/XSLT/namespace"
xmlns:test="http://xmlsoft.org/XSLT/"
extension-element-prefixes="exslt math date func set str dyn saxon xalanredirect xt libxslt test"
exclude-result-prefixes="math str">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/">
<xsl:apply-templates />
<xsl:if test="count(/article/additional-links/a) &gt; 0">
<xsl:element name="h2">Ссылки по теме</xsl:element>
<xsl:element name="ul">
<xsl:for-each select="/article/additional-links/a">
<xsl:element name="li">
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="@href" /></xsl:attribute>
<xsl:attribute name="title"><xsl:value-of select="@title" /></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:if>
<xsl:element name="p">Источник: <xsl:element name="a"><xsl:attribute name="href">http://kuroikaze85.wordpress.com/</xsl:attribute>Механический мир</xsl:element></xsl:element>
</xsl:template>
<xsl:template match="sourcecode">
<xsl:element name="pre">
<xsl:attribute name="lang"><xsl:value-of select="@lang"/></xsl:attribute>
<xsl:attribute name="escaped">true</xsl:attribute>
<xsl:if test="@line"><xsl:attribute name="line"><xsl:value-of select="@line"/></xsl:attribute></xsl:if>
<!-- No highlighting on Nodejs.ru -->
<xsl:value-of select="." disable-output-escaping="no" />
</xsl:element>
</xsl:template>
<xsl:template match="more-tag">
<xsl:comment>more</xsl:comment>
</xsl:template>
<!-- Sections and subsections -->
<xsl:template match="section">
<xsl:element name="h2">
<xsl:value-of select="normalize-space(@name)" />
</xsl:element>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="subsection">
<xsl:element name="h3">
<xsl:value-of select="normalize-space(@name)" />
</xsl:element>
<xsl:apply-templates />
</xsl:template>
<!-- text elements -->
<xsl:template match="p">
<xsl:element name="p"><xsl:apply-templates /></xsl:element>
</xsl:template>
<xsl:template match="code">
<xsl:element name="code"><xsl:value-of select="." /></xsl:element>
</xsl:template>
<xsl:template match="a">
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="@href" /></xsl:attribute>
<xsl:attribute name="target"><xsl:value-of select="@target" /></xsl:attribute>
<xsl:value-of select="normalize-space(.)" />
</xsl:element>
</xsl:template>
<xsl:template match="img">
<xsl:element name="img">
<xsl:attribute name="src"><xsl:value-of select="@src" /></xsl:attribute>
<xsl:attribute name="alt"><xsl:value-of select="@alt" /></xsl:attribute>
<xsl:attribute name="title"><xsl:value-of select="@title" /></xsl:attribute>
</xsl:element>
</xsl:template>
<xsl:template match="article-link">
<xsl:element name="a">
<xsl:attribute name="href">http://kuroikaze85.wordpress.com<xsl:value-of select="@href" /></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="normalize-space(.)" />
</xsl:element>
</xsl:template>
<xsl:template match="em">
<xsl:element name="em"><xsl:value-of select="." /></xsl:element>
</xsl:template>
<xsl:template match="ul">
<xsl:element name="ul"><xsl:apply-templates /></xsl:element>
</xsl:template>
<xsl:template match="li">
<xsl:element name="li"><xsl:apply-templates /></xsl:element>
</xsl:template>
<xsl:template match="b">
<xsl:element name="b"><xsl:apply-templates /></xsl:element>
</xsl:template>
<xsl:template match="tt">
<xsl:element name="tt"><xsl:apply-templates /></xsl:element>
</xsl:template>
<xsl:template match="pre">
<xsl:element name="pre"><xsl:value-of select="." disable-output-escaping="yes" /></xsl:element>
</xsl:template>
<!-- Github-specific tags -->
<xsl:template match="github-user">
<xsl:element name="a">
<xsl:attribute name="href">http://github.com/<xsl:value-of select="." /></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="github-project">
<xsl:element name="a">
<xsl:attribute name="href">http://github.com/<xsl:value-of select="." /></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<!-- Habrahabr-specific tags -->
<xsl:template match="habrauser">
<xsl:element name="a">
<xsl:attribute name="href">http://<xsl:value-of select="." />.habrahabr.com</xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="habrapost">
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="@href" /></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="normalize-space(.)" />
</xsl:element>
</xsl:template>
<!-- Node.js specific tags -->
<xsl:template match="node-function">
<xsl:element name="code">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="node-module">
<!-- <xsl:element name="a">
<xsl:attribute name="href">http://nodejs.org/api.html#<xsl:value-of select="."></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>-->
<xsl:value-of select="." />
<!--</xsl:element>-->
</xsl:template>
<!-- Hide elements -->
<xsl:template match="article/title"/>
<xsl:template match="additional-links"/>
</xsl:stylesheet>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exslt="http://exslt.org/common"
xmlns:math="http://exslt.org/math"
xmlns:date="http://exslt.org/dates-and-times"
xmlns:func="http://exslt.org/functions"
xmlns:set="http://exslt.org/sets"
xmlns:str="http://exslt.org/strings"
xmlns:dyn="http://exslt.org/dynamic"
xmlns:saxon="http://icl.com/saxon"
xmlns:xalanredirect="org.apache.xalan.xslt.extensions.Redirect"
xmlns:xt="http://www.jclark.com/xt"
xmlns:libxslt="http://xmlsoft.org/XSLT/namespace"
xmlns:test="http://xmlsoft.org/XSLT/"
extension-element-prefixes="exslt math date func set str dyn saxon xalanredirect xt libxslt test"
exclude-result-prefixes="math str">
<xsl:output omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
<xsl:apply-templates />
<xsl:if test="count(/article/additional-links/a) &gt; 0">
<xsl:element name="h2">Ссылки по теме</xsl:element>
<xsl:element name="ul">
<xsl:for-each select="/article/additional-links/a">
<xsl:element name="li">
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="@href" /></xsl:attribute>
<xsl:attribute name="title"><xsl:value-of select="@title" /></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:if>
</xsl:template>
<xsl:template match="sourcecode">
[sourcecode lang="<xsl:value-of select="@lang"/>"<xsl:if test="@line"> line="<xsl:value-of select="@line"/>"</xsl:if><xsl:if test="@highlight"> highlight="<xsl:value-of select="@highlight"/>"</xsl:if>]
<xsl:value-of select="." disable-output-escaping="yes" />
[/sourcecode]
</xsl:template>
<xsl:template match="more-tag">
<xsl:comment>more</xsl:comment>
</xsl:template>
<xsl:template match="section">
<xsl:element name="h2">
<xsl:value-of select="normalize-space(@name)" />
</xsl:element>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="subsection">
<xsl:element name="h3">
<xsl:value-of select="normalize-space(@name)" />
</xsl:element>
<xsl:apply-templates />
</xsl:template>
<xsl:template match="p">
<xsl:element name="p"><xsl:apply-templates /></xsl:element>
</xsl:template>
<xsl:template match="ul">
<xsl:element name="ul"><xsl:apply-templates /></xsl:element>
</xsl:template>
<xsl:template match="li">
<xsl:element name="li"><xsl:apply-templates /></xsl:element>
</xsl:template>
<xsl:template match="code">
<xsl:element name="code"><xsl:value-of select="." /></xsl:element>
</xsl:template>
<xsl:template match="a">
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="@href" /></xsl:attribute>
<xsl:attribute name="target"><xsl:value-of select="@target" /></xsl:attribute>
<xsl:value-of select="normalize-space(.)" />
</xsl:element>
</xsl:template>
<xsl:template match="article-link">
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="@href" /></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="normalize-space(.)" />
</xsl:element>
</xsl:template>
<xsl:template match="img">
<xsl:element name="img">
<xsl:attribute name="src"><xsl:value-of select="@src" /></xsl:attribute>
<xsl:attribute name="alt"><xsl:value-of select="@alt" /></xsl:attribute>
<xsl:attribute name="title"><xsl:value-of select="@title" /></xsl:attribute>
</xsl:element>
</xsl:template>
<xsl:template match="em">
<xsl:element name="em"><xsl:apply-templates /></xsl:element>
</xsl:template>
<xsl:template match="b">
<xsl:element name="b"><xsl:apply-templates /></xsl:element>
</xsl:template>
<xsl:template match="tt">
<xsl:element name="tt"><xsl:apply-templates /></xsl:element>
</xsl:template>
<xsl:template match="pre">
<xsl:element name="pre"><xsl:value-of select="." disable-output-escaping="yes" /></xsl:element>
</xsl:template>
<!-- Github-specific tags -->
<xsl:template match="github-user">
<xsl:element name="a">
<xsl:attribute name="href">http://github.com/<xsl:value-of select="." /></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="github-project">
<xsl:element name="a">
<xsl:attribute name="href">http://github.com/<xsl:value-of select="." /></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<!-- Habrahabr-specific tags -->
<xsl:template match="habrauser">
<xsl:element name="a">
<xsl:attribute name="href">http://<xsl:value-of select="." />.habrahabr.com</xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<xsl:template match="habrapost">
<xsl:element name="a">
<xsl:attribute name="href"><xsl:value-of select="@href" /></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:value-of select="normalize-space(.)" />
</xsl:element>
</xsl:template>
<!-- Node.js specific tags -->
<xsl:template match="node-function">
<xsl:element name="code">
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="node-module">
<!-- <xsl:element name="a">
<xsl:attribute name="href">http://nodejs.org/api.html#<xsl:value-of select="."></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>-->
<xsl:apply-templates />
<!--</xsl:element>-->
</xsl:template>
<!-- Hide elements -->
<xsl:template match="article/title"/>
<xsl:template match="additional-links"/>
</xsl:stylesheet>
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE article [
<!ENTITY mdash "&#8212;"><!-- em dash -->
]>
<article>
<title>Ndb - отладчик для Node.js</title>
<p>Node.js продолжает обрастать инструментами для разработки ) Совсем недавно Github-пользователь <github-user>smtlaissezfaire</github-user> выпустил альфа-версию своего отладчика <a href="http://github.com/smtlaissezfaire/ndb" target="_blank">ndb</a>.</p>
<more-tag/>
<section name="Пошаговое выполнение кода">
<p>Чтобы запустить скрипт в node в режиме отладки, надо добавить ключ <code>--debug-brk</code>:</p>
<sourcecode lang="bash">
<![CDATA[node --debug-brk script.js]]>
</sourcecode>
<p>Я попробую пройтись дебаггером по версии express, на которой делал поисковик. Посмотрим каково это. Запускаем сайт в режиме отладки:</p>
<sourcecode lang="bash">
<![CDATA[cd spider/site
node --debug-brk script.js &]]>
</sourcecode>
<p>Node скажет что отладчик слушает на порту 5858. Запускаем ndb, подключаемся. Флаг <code>--debug-brk</code> останавливает выполнение скрипта в самом начале, и мы сможем пройтись по нему самостоятельно. Командой <code>list</code> можно посмотреть что именно мы собрались выполнять:</p>
<pre>
<![CDATA[ndb> list
1 (function (exports, require, module, __filename, __dirname) { var kiwi = require('kiwi')
,
2 limestone = require('./limestone'),
3 Do = require('./do'),
4 sys = require('sys'),
5 settings = require('../settings'),
6 couch = require('../node-couch').CouchDB;
7
8 kiwi.require('express');
9]]>
</pre>
<p>Если теперь мы будем нажимать <code>s</code> (step), скрипт будет выполняться построчно - мы увидим как отрабатывают начальные <node-function>require</node-function>. В любой момент можно вывести значение переменной с помощью <code>p</code> (print):</p>
<pre>
<![CDATA[ndb> p rest
=> #<an Array>]]>
</pre>
<p>Или выполнить произвольный код с помощью <code>e</code> (eval):</p>
<pre>
<![CDATA[ndb> e rest = []
=> #<an Array>]]>
</pre>
<p>Содержимое объектов удобнее просматривать с помощью <code>e JSON.stringify</code>:</p>
<pre>
<![CDATA[ndb> e JSON.stringify(id)
=> "kiwi"]]>
</pre>
<p>Здесь мы находимся в загрузчике модуля kiwi.</p>
</section>
<section name="Точки останова">
Node.js позволяет задать точки останова прямо в коде с помощью инструкции <code>debugger</code>. Попробуем оставить такую точку в функции, осуществляющей поиск с помощью limestone:
<sourcecode lang="JavaScript" line="24" highlight="30">
<![CDATA[ limestone.connect(9312, function(err) {
if (err) {
// sys.puts('Connection error: ' . err);
this.render('results.html.haml', {'locals': {'header': 'Search results for "' + query + '"', 'query': query, 'results': 'Connection error'}});
}
//sys.puts('Connected, sending query');
debugger;
limestone.query({'query': query, maxmatches: 20}, function(err, answer) {
// Обработка результатов поиска
}]]>
</sourcecode>
<p>Запускаем скрипт снова, запускаем ndb. По умолчанию отладчик останавливает выполнение, так что запускаем его командой <code>continue</code>. Мы должны увидеть стандартное сообщение Express:</p>
<pre>
<![CDATA[ndb> continue
deprecation warning: process.mixin will be removed from node-core future releases.
Express started at http://192.168.175.128:8000/ in development mode]]>
</pre>
<p>Теперь открываем сайт, делаем поиск и возвращаемся в дебаггер. В нём мы должны увидеть что скрипт остановлен:</p>
<pre>
<![CDATA[Breakpoint at 30:4 (in function undefined)
debugger;
ndb>]]>
</pre>
<p>Теперь мы можем отладить эту функцию - проверить переменные, выполнить код, пройти её по шагам. Когда закончим, можно запустить дальнейшее выполнение с помощью <code>continue</code>.</p>
<p>Хотя отладчик и находится пока в альфа-версии, и периодически вылетает с segmentation fault, это всё равно полезный инструмент. Особенно для разбора сложного или необычного кода вроде <article-link href="/2010/04/08/fab-nodejs-framework/">(fab)</article-link> :)</p>
</section>
<additional-links>
<a href="http://github.com/smtlaissezfaire/ndb">Страница отладчика на Github</a>
</additional-links>
</article>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment