Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Thomas Beutel http://www.beutelevision.com

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Convert HTML entities

Include this to convert those ampersands and angle brackets to proper entities. It can help with XML too but remember there are lots of other entities that need converting.

use HTML::Entities;

...

$html = encode_entities($string, '<>&"');

XSLT Alternating Colors for HTML Table Rows

This attaches a style attribute to the tr tag, but only on every other tag.

<xsl:template match="my_repeating_item">
<tr>
  <xsl:if test="position() mod 2 != 1">
    <xsl:attribute  name="style">background-color:#dddddd</xsl:attribute>
  </xsl:if>
  <xsl:apply-templates/>
</tr>
<xsl:template>

Convert Emacs Region to HTML

Sometimes while I'm editing, I want to convert a region to HTML. I use Perl's HTML::FromText. First I create this script and call it tohtml.

#!/usr/bin/perl

 use HTML::FromText;
 use strict;

 my $text = join '',<>;
 my $html = text2html($text, urls => 1, email => 1, bold=>1, paras => 1, allcaps => 1, tables => 1, bullets => 1, underline=>1);

print $html;


Then from within Emacs, I select the region and do

C-x ESC-1 ESC-| tohtml RET

Write list as XML to HTML form - Javascript

Suppose that you have a draggable list as described at http://tool-man.org/examples/sorting.html. To send that list from a form, do

function listToXml(){
	var vList = document.getElementById('phonetic');
        var vLen  = vList.childNodes.length;
	var vXml  = "<ul>";
        for(var i = 0; i < vLen - 1; i++){
	  if(vList.childNodes[i].innerHTML){
	    vXml = vXml + '<li>' + vList.childNodes[i].innerHTML + '</li>';
	  }
        }
	vXml = vXml + '</ul>';
	document.myform.listfield.value = vXml;
}

...

	<ul id="phonetic" class="sortable boxy" style="margin-left: 1em;">
		<li>alpha</li>
		<li>bravo</li>
		<li>charlie</li>
		<li>delta</li>
		<li>echo</li>
		<li>foxtrot</li>
        </ul>

<form name="myform">
<input type="text" name="listfield" value="" size="60">
<input type="button" value="submit2" name="submit2" onClick="listToXml()">
</form>

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS