Making examples

<p:library xmlns:p="http://www.w3.org/ns/xproc"
	   xmlns:c="http://www.w3.org/ns/xproc-step"
	   xmlns:cx="http://xmlcalabash.com/ns/extensions"
	   xmlns:xs="http://www.w3.org/2001/XMLSchema"
	   xmlns:pl="http://www.w3.org/XML/XProc/docs/library">

<p:import href="http://xmlcalabash.com/extension/steps/library-1.0.xpl"/>
<p:import href="/home/ndw/xmlcalabash.com/library/depends.xpl"/>

<p:pipeinfo xml:id="xpl-sources">
  <cx:targets>
    <cx:target>file</cx:target>
    <cx:target>filebin</cx:target>
  </cx:targets>
</p:pipeinfo>

<p:pipeinfo xml:id="xml-sources">
  <cx:targets>
    <cx:target>choose</cx:target>
    <cx:target>for-each</cx:target>
    <cx:target>group</cx:target>
    <cx:target>identity</cx:target>
    <cx:target>input-doc</cx:target>
    <cx:target>input-port</cx:target>
    <cx:target>input-select</cx:target>
    <cx:target>pipeline-library</cx:target>
    <cx:target>pipeline</cx:target>
    <cx:target>simple-default</cx:target>
    <cx:target>simple-explicit</cx:target>
    <cx:target>trycatch</cx:target>
    <cx:target>viewport</cx:target>
    <cx:target>xinclude</cx:target>
    <cx:target>xpathcontext</cx:target>
    <cx:target>xquery</cx:target>
    <cx:target>xslt-empty</cx:target>
    <cx:target>xslt</cx:target>
  </cx:targets>
</p:pipeinfo>

<p:pipeinfo xml:id="copy-sources">
  <cx:targets>
    <cx:target>pipeline</cx:target>
    <cx:target>pipeline-library</cx:target>
    <cx:target>simple-default</cx:target>
    <cx:target>simple-explicit</cx:target>
    <cx:target>xquery</cx:target>
  </cx:targets>
</p:pipeinfo>

<p:declare-step name="main" type="pl:build-examples">
  <p:for-each>
    <p:iteration-source select="id('xml-sources')//cx:target">
      <p:document href="#"/>
    </p:iteration-source>
    
    <pl:strip>
      <p:with-option name="source"
		     select="resolve-uri(concat(., '.xml'), base-uri(.))"/>
      <p:with-option name="target"
		     select="resolve-uri(concat(., '.txt'), base-uri(.))"/>
    </pl:strip>
  </p:for-each>

  <p:for-each>
    <p:iteration-source select="id('xpl-sources')//cx:target">
      <p:document href="#"/>
    </p:iteration-source>
    
    <pl:strip>
      <p:with-option name="source"
		     select="resolve-uri(concat(., '.xpl'), base-uri(.))"/>
      <p:with-option name="target"
		     select="resolve-uri(concat(., '.txt'), base-uri(.))"/>
    </pl:strip>
  </p:for-each>

  <p:for-each>
    <p:iteration-source select="id('copy-sources')//cx:target">
      <p:document href="#"/>
    </p:iteration-source>
    
    <pl:copy>
      <p:with-option name="source"
		     select="resolve-uri(concat(., '.xml'), base-uri(.))"/>
      <p:with-option name="target"
		     select="resolve-uri(concat(., '.txt'), base-uri(.))"/>
    </pl:copy>
  </p:for-each>
</p:declare-step>

<p:declare-step name="main" type="pl:copy">
  <p:option name="source" required="true"/>
  <p:option name="target" required="true"/>

  <p:string-replace match="/cx:depends/cx:target/text()">
    <p:input port="source">
      <p:inline>
	<cx:depends>
	  <cx:target>target</cx:target>
	  <cx:source>source</cx:source>
	</cx:depends>
      </p:inline>
    </p:input>
    <p:with-option name="replace" select="concat('"',$target,'"')">
      <p:empty/>
    </p:with-option>
  </p:string-replace>

  <p:string-replace match="/cx:depends/cx:source/text()">
    <p:with-option name="replace" select="concat('"',$source,'"')">
      <p:empty/>
    </p:with-option>
  </p:string-replace>

  <cx:out-of-date/>

  <p:choose>
    <p:when test="/c:result = 'true'">
      <cx:message>
	<p:with-option name="message"
		       select="concat('Copying: ', $target)">
	  <p:empty/>
	</p:with-option>
      </cx:message>
  
      <p:exec name="exec" result-is-xml="false" source-is-xml="false"
	      command="/bin/cp">
	<p:input port="source">
	  <p:empty/>
	</p:input>
	<p:with-option name="args" select="concat($source,' ',$target)"/>
      </p:exec>
      
      <p:sink/>
    </p:when>
    <p:otherwise>
      <cx:message>
	<p:input port="source"><p:empty/></p:input>
	<p:with-option name="message"
		       select="concat('Up-to-date: ', $target)">
	  <p:empty/>
	</p:with-option>
      </cx:message>
      <p:sink/>
    </p:otherwise>
  </p:choose>
</p:declare-step>

<p:declare-step name="main" type="pl:strip">
  <p:option name="source" required="true"/>
  <p:option name="target" required="true"/>

  <p:string-replace match="/cx:depends/cx:target/text()">
    <p:input port="source">
      <p:inline>
	<cx:depends>
	  <cx:target>target</cx:target>
	  <cx:source>source</cx:source>
	</cx:depends>
      </p:inline>
    </p:input>
    <p:with-option name="replace" select="concat('"',$target,'"')">
      <p:empty/>
    </p:with-option>
  </p:string-replace>

  <p:string-replace match="/cx:depends/cx:source/text()">
    <p:with-option name="replace" select="concat('"',$source,'"')">
      <p:empty/>
    </p:with-option>
  </p:string-replace>

  <cx:out-of-date/>

  <p:choose>
    <p:when test="/c:result = 'true'">
      <p:string-replace match="/c:request/@href">
	<p:input port="source">
	  <p:inline exclude-inline-prefixes="cx xs pl">
	    <c:request method="get" override-content-type="text/plain"
		       href=""/>
	  </p:inline>
	</p:input>
	<p:with-option name="replace" select="concat('"',$source,'"')">
	  <p:empty/>
	</p:with-option>
      </p:string-replace>

      <p:http-request/>

      <cx:message>
	<p:with-option name="message"
		       select="concat('Updating: ', $target)">
	  <p:empty/>
	</p:with-option>
      </cx:message>
  
      <p:exec name="exec" result-is-xml="false" source-is-xml="false"
	      command="/opt/local/bin/perl" args="strip.pl">
      </p:exec>
      
      <p:unescape-markup/>

      <p:store method="text" name="store">
	<p:with-option name="href" select="$target"/>
      </p:store>

      <p:sink>
	<p:input port="source">
	  <p:pipe step="store" port="result"/>
	</p:input>
      </p:sink>
    </p:when>
    <p:otherwise>
      <cx:message>
	<p:input port="source"><p:empty/></p:input>
	<p:with-option name="message"
		       select="concat('Up-to-date: ', $target)">
	  <p:empty/>
	</p:with-option>
      </cx:message>
      <p:sink/>
    </p:otherwise>
  </p:choose>
</p:declare-step>

<p:declare-step name="main" type="pl:strip-brute">
  <p:option name="source" required="true"/>
  <p:option name="target" required="true"/>

  <p:string-replace match="/c:request/@href">
    <p:input port="source">
      <p:inline exclude-inline-prefixes="cx xs pl">
	<c:request method="get" override-content-type="text/plain"
		   href=""/>
      </p:inline>
    </p:input>
    <p:with-option name="replace" select="concat('"',$source,'"')">
      <p:empty/>
    </p:with-option>
  </p:string-replace>

  <p:http-request/>

  <cx:message>
    <p:with-option name="message"
		   select="concat('Updating ', $target)">
      <p:empty/>
    </p:with-option>
  </cx:message>

  <p:exec name="exec" result-is-xml="false" source-is-xml="false"
	  command="/opt/local/bin/perl" args="strip.pl">
  </p:exec>

  <p:unescape-markup/>

  <p:store method="text">
    <p:with-option name="href" select="$target"/>
  </p:store>
</p:declare-step>

</p:library>