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

« Newer Snippets
Older Snippets »
Showing 1-10 of 20 total  RSS 

Code 2 HTML

This is a simple python program that reads a file (code), and replaces line breaks and spaces with appropriate tag and entity.

import sys

line_break='<br>'
nb_space='&#160;'

def toHtml(f,out):
    try:
        fo=open(out,'w')
    except IOError:
        print 'output file error!'
    else:
        for line in f:
            line=line.replace('\t',nb_space*8)
            line=line.replace(' ',nb_space)
            line=line.replace('\n',line_break)
            fo.write(line)
        fo.close()
        

if len(sys.argv) == 1:
    print("at least one argument / input filename / required")
    sys.exit()
if len(sys.argv) > 3:
    print"too many arguments"
    sys.exit()       
try:
    f=open(sys.argv[1],'r')
except IOError:
    print 'cannot open file ', sys.argv[1]
else:
    if len(sys.argv) == 2:
        out=f.name + '.html'
    else:
        out=sys.argv[2]
    toHtml(f, out)
    f.close()

Reference of keyCodes

    switch (oEvent.keyCode) {
       case 38: //up arrow  
       case 40: //down arrow
       case 37: //left arrow
       case 39: //right arrow
       case 33: //page up  
       case 34: //page down  
       case 36: //home  
       case 35: //end                  
       case 13: //enter  
       case 9: //tab  
       case 27: //esc  
       case 16: //shift  
       case 17: //ctrl  
       case 18: //alt  
       case 20: //caps lock
       case 8: //backspace  
       case 46: //delete
           return true;
           break;

       default: 

Note: When capturing combination keys there is dedicated boolean attributes for each of the special keys (CTRL, SHIFT, ALT).
Reference: Make Life Easy With Autocomplete Textboxes [JavaScript & AJAX Tutorials] [sitepoint.com]

Adding new files to your github repository

Here is a set of instructions to apply new files to my github repository which is called projectx.

Before you start make sure you don't already have the repository name listed as a directory in the local current directory.

1) copy the remote repository to your local machine
syntax: git clone [uri] # eg.
git clone git@github.com:jrobertson/projectx.git

1.5) cd into the newly created local repository eg.
cd projectx

2) copy the local file to the local repository directory
eg.
cp ../projectx2/feed.rb .

3) add the local files to the local repository
syntax: git add [file] # eg.
git add feed.rb

4) Inform the git system that you have completed the required changes for this session.
git commit -a # add a message associated with this file revision

5) copy the new local repository files back to the remote repository.
git push # updates the changes back to the server

Note: The text with the square-brackets should be replaced with your own values.

Reference: A tour of git: the basics [cworth.org]

Format Ruby code in HTML

This code uses the Ruby gem 'syntax' to create an XML file containing the HTML tags around the code, which is then transformed into an HTML file. The working example was first built using coding examples from Howto format ruby code for blogs [wolfman.com] and Formatting Ruby and HTML code for blog posting [blogspot.com]

#!/usr/bin/ruby

#file: ruby2html.rb 

require 'rubygems'
require 'syntax/convertors/html'
require 'projxslt' # <- this is my own class to do an XSLT transform 
require 'rexml/document'
include REXML

class Ruby2Html
  def initialize(rubyfile, htmlfile)
    code = File.read(rubyfile)
    convertor = Syntax::Convertors::HTML.for_syntax "ruby"
    code_html = convertor.convert(code)
    
    tempfile = '../temp/ruby2html.xml'
    xslfile = '../ruby2html/ruby2html.xsl'
    save_file(tempfile, code_html)
    
    px = Projxslt.new(tempfile, xslfile)
    buffer = px.transform()
    save_file(htmlfile, buffer)
    
  end
  
  def save_file(filename, buffer)
    file = File.new(filename, 'w')
    file.puts buffer
    file.close
  end
end

if __FILE__ == $0
  r2h = Ruby2Html.new('ruby2html.rb', '../temp/ruby2html.html')
  puts 'completed'
end

file: ruby2html.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/1999/xhtml"
                version="1.0">

  <xsl:output method="html" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
          doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
          encoding="ISO-8859-1"/> 

	<xsl:template match="/">
	<xsl:element name="html">

        <head>
          <title>Sample code</title>
          <link rel="stylesheet" type="text/css" href="ruby2html.css" />
	</head>

	<body>
          <div id="wrap">
          <xsl:apply-templates />
          </div>
	</body> 

	</xsl:element>
	</xsl:template>

	<xsl:template match="pre">
	  <xsl:copy-of select="."/>
	</xsl:template>

</xsl:stylesheet>

Here's the output from the formatted Ruby HTML code [twitxr.com]

Referemce: Syntax Manual [rubyforge.org]

Code Of A Game

This Is A Code Of A Game.

Coppy It Into Notepad And Then Save It As Game.bat

Warning!
Make Sure It Says "All Files (*.*)" And Not "Text Document (.txt)" At The Bottom.
The Code Is:
@echo off
goto verybegingame
: verybegingame
echo Welcome To This Game!
title Game Start
echo Press Any Key For Stuff.
pause >nul
echo Loading...
ping localhost 6 >nul
echo.. Loading Comple!
pause >nul
cls
echo Rules:
title Game Rules
echo..You Answer Each Question.
echo. If It Is Correct, You Will Move On.
echo If It is InCorrect, The Program Will
echo Exit And You Will Need To Try Again.
echo.. If You Get All The Questions Right, You
echo Will Receave A Speical Code.
echo Enter THe Code For A Suprse!
echo...
echo Good Luck!
echo.....
echo Press A Key Of An Option, And Then 
echo Press 'Enter'.
echo...
echo                        1 = Play Game          2 = Enter Code
SET /P "onechoose1=Enter 1 or 2 option here:"
If %onechoose1%= 1 then goto playgame
If %onechoose1%= 2 then goto entercode
:playgame
cls
echo What Is My Name?
echo A.Jonathan
echo B.Sam
echo C.Johnn
SET /p "q1=Enter Option:"
If NOT%q1%= A then exit
:entercode
echo  Enter Code
SET /P "codeenter= "
If %entercode%=code... goto prize
:prize



All Of ThAT...
MAKE MORE QUESTIONS.
etc.

Morse decode using sed

This is a short Morse code decoder written as a shellscript using sed.

The Morse coded text should be in $text, and should be written with spaces between the letters.

echo $text\  | tr . 0 | sed -e {s/0----\ /1/g} -e {s/00---\ /2/g} -e {s/000--\ /3/g} -e {s/000-\ /4/g} -e {s/00000\ /5/g} -e {s/-0000\ /6/g} -e {s/--000\ /7/g} -e {s/---00\ /8/g} -e {s/----0\ /9/g} -e {s/-----\ /0/g} \
	| sed -e {s/-0-0\ /c/g} -e {s/-000\ /b/g} -e {s/00-0\ /f/g} -e {s/0000\ /h/g} -e {s/0---\ /j/g} -e {s/0-00\ /l/g} -e {s/0--0\ /p/g} -e {s/--0-\ /q/g} -e {s/000-\ /v/g} -e {s/-00-\ /x/g} -e {s/-0--\ /y/g} -e {s/--00\ /z/g} \
	| sed -e {s/0--\ /w/g} -e {s/-00\ /d/g} -e {s/--0\ /g/g} -e {s/-0-\ /k/g} -e {s/---\ /o/g} -e {s/0-0\ /r/g} -e {s/000\ /s/g} -e {s/00-\ /u/g} \
	| sed -e {s/0-\ /a/g} -e {s/00\ /i/g} -e {s/--\ /m/g} -e {s/-0\ /n/g} \
	| sed -e {s/0\ /e/g} -e {s/-\ /t/g}

Generate Ruby code using XML and XSL

This XSL code transforms an XML file into a basic Ruby file. See the previous post ('Transforming an XML file into an XSL file' http://snipr.com/1usrh [dzone.com]) for an example of the XML used.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>

  <xsl:template match="recordx">
  <xsl:variable name="project" select="summary/project" />
  <xsl:variable name="parent_element" select="summary/parent_element" />
  <xsl:text>#!/usr/bin/ruby
#file: </xsl:text><xsl:value-of select="$project"/><xsl:text>.rb

require 'recordx'

class </xsl:text><xsl:value-of select="$project"/><xsl:text> &lt; RecordX

  def initialize()
    @project = "</xsl:text><xsl:value-of select="$project"/><xsl:text>"
    @parent_element = '</xsl:text><xsl:value-of select="$parent_element"/><xsl:text>fields'
    @proj = Projxml.new
    @doc_vrecord = initialize_xml_class(SERVER_URL + '/' + @project + '/class_template.xml')
    @doc_file = get_doc()
  end
  
end

if __FILE__ == $0
    r = </xsl:text><xsl:value-of select="$project"/><xsl:text>.new()
end
  </xsl:text>
  </xsl:template>
  
</xsl:stylesheet>

Translates Morse code from a mobile phone style keypad into plain English

// Translates Morse code specially input from a Nokia 3510i or Nokia 6600 to plain English. I primarily used the button 1 for a dot, 2 for a dash and 5 for the terminator. I later extended the flexibility of the inputs by allowing 4 to be a dot, 7 a dash and 9 as the terminator. eg. ghi = dots pqrs = dashes wxyz = terminators and spaces.

#!/usr/bin/ruby
#filename: morsecode.rb

require 'net/http'
require 'rexml/document'

include REXML

class Morsecode
  def initialize
    @doc_mlookup = load_lookup('http://jamesrobertson.eu/p/xml/', 'morsecode.xml')
    @doc_charmcode = load_lookup('http://jamesrobertson.eu/p/xml/', 'charmcode.xml')
  end

  def load_lookup(url, filename)
    xml_data = Net::HTTP.get_response(URI.parse(url + filename)).body
    Document.new(xml_data)
  end

  def decode_string(doc, a_buffer)
    pstring = ''
    a_buffer.each do |word|
      pstring += doc.root.elements["code[@encode='" + word + "']"].text
    end
    pstring
  end

  def decode_kstring(buffer)
    decode_string(@doc_charmcode, buffer.scan(/./))
  end

  def decode_mstring(buffer)
    decode_string(@doc_mlookup, buffer.split('4'))
  end

  def decode(buffer)
    decode_mstring(decode_kstring(buffer)) if buffer.length > 0
  end
end

#test
a = Morsecode.new
puts a.decode('gwpwrwgw')


US Area Codes PHP Array


<?php
//This is just a little array filled with all the area codes
//for each state. Useful for verifying phone numbers.
//Apologies for my screen name, I didn't want to bother registering
//for an account... But you guys helped me out alot so I felt
//like I had to give back. :)
$areacodes = array(780 => "AB",
403 => "AB",
907 => "AK",
205 => "AL",
256 => "AL",
334 => "AL",
251 => "AL",
870 => "AR",
501 => "AR",
479 => "AR",
480 => "AZ",
623 => "AZ",
928 => "AZ",
602 => "AZ",
520 => "AZ",
628 => "CA",
341 => "CA",
764 => "CA",
925 => "CA",
909 => "CA",
562 => "CA",
661 => "CA",
657 => "CA",
510 => "CA",
650 => "CA",
949 => "CA",
760 => "CA",
415 => "CA",
951 => "CA",
752 => "CA",
831 => "CA",
209 => "CA",
669 => "CA",
408 => "CA",
559 => "CA",
626 => "CA",
442 => "CA",
530 => "CA",
916 => "CA",
707 => "CA",
627 => "CA",
714 => "CA",
310 => "CA",
323 => "CA",
213 => "CA",
424 => "CA",
747 => "CA",
818 => "CA",
858 => "CA",
935 => "CA",
619 => "CA",
805 => "CA",
369 => "CA",
720 => "CO",
303 => "CO",
970 => "CO",
719 => "CO",
203 => "CT",
959 => "CT",
475 => "CT",
860 => "CT",
202 => "DC",
302 => "DE",
689 => "FL",
407 => "FL",
239 => "FL",
836 => "FL",
727 => "FL",
321 => "FL",
754 => "FL",
954 => "FL",
352 => "FL",
863 => "FL",
904 => "FL",
386 => "FL",
561 => "FL",
772 => "FL",
786 => "FL",
305 => "FL",
861 => "FL",
941 => "FL",
813 => "FL",
850 => "FL",
478 => "GA",
770 => "GA",
470 => "GA",
404 => "GA",
706 => "GA",
678 => "GA",
912 => "GA",
229 => "GA",
671 => "GU",
808 => "HI",
515 => "IA",
319 => "IA",
563 => "IA",
641 => "IA",
712 => "IA",
208 => "ID",
217 => "IL",
282 => "IL",
872 => "IL",
312 => "IL",
773 => "IL",
464 => "IL",
708 => "IL",
815 => "IL",
224 => "IL",
847 => "IL",
618 => "IL",
309 => "IL",
331 => "IL",
630 => "IL",
765 => "IN",
574 => "IN",
260 => "IN",
219 => "IN",
317 => "IN",
812 => "IN",
913 => "KS",
785 => "KS",
316 => "KS",
620 => "KS",
327 => "KY",
502 => "KY",
859 => "KY",
606 => "KY",
270 => "KY",
504 => "LA",
985 => "LA",
225 => "LA",
318 => "LA",
337 => "LA",
774 => "MA",
508 => "MA",
781 => "MA",
339 => "MA",
857 => "MA",
617 => "MA",
978 => "MA",
351 => "MA",
413 => "MA",
443 => "MD",
410 => "MD",
280 => "MD",
249 => "MD",
969 => "MD",
240 => "MD",
301 => "MD",
207 => "ME",
383 => "ME",
517 => "MI",
546 => "MI",
810 => "MI",
278 => "MI",
313 => "MI",
586 => "MI",
248 => "MI",
734 => "MI",
269 => "MI",
906 => "MI",
989 => "MI",
616 => "MI",
231 => "MI",
679 => "MI",
947 => "MI",
612 => "MN",
320 => "MN",
651 => "MN",
763 => "MN",
952 => "MN",
218 => "MN",
507 => "MN",
636 => "MO",
660 => "MO",
975 => "MO",
816 => "MO",
314 => "MO",
557 => "MO",
573 => "MO",
417 => "MO",
670 => "MP",
601 => "MS",
662 => "MS",
228 => "MS",
406 => "MT",
336 => "NC",
252 => "NC",
984 => "NC",
919 => "NC",
980 => "NC",
910 => "NC",
828 => "NC",
704 => "NC",
701 => "ND",
402 => "NE",
308 => "NE",
603 => "NH",
908 => "NJ",
848 => "NJ",
732 => "NJ",
551 => "NJ",
201 => "NJ",
862 => "NJ",
973 => "NJ",
609 => "NJ",
856 => "NJ",
505 => "NM",
957 => "NM",
702 => "NV",
775 => "NV",
315 => "NY",
518 => "NY",
716 => "NY",
585 => "NY",
646 => "NY",
347 => "NY",
718 => "NY",
212 => "NY",
516 => "NY",
917 => "NY",
845 => "NY",
631 => "NY",
607 => "NY",
914 => "NY",
216 => "OH",
330 => "OH",
234 => "OH",
567 => "OH",
419 => "OH",
380 => "OH",
440 => "OH",
740 => "OH",
614 => "OH",
283 => "OH",
513 => "OH",
937 => "OH",
918 => "OK",
580 => "OK",
405 => "OK",
503 => "OR",
971 => "OR",
541 => "OR",
814 => "PA",
717 => "PA",
570 => "PA",
358 => "PA",
878 => "PA",
835 => "PA",
484 => "PA",
610 => "PA",
445 => "PA",
267 => "PA",
215 => "PA",
724 => "PA",
412 => "PA",
939 => "PR",
787 => "PR",
401 => "RI",
843 => "SC",
864 => "SC",
803 => "SC",
605 => "SD",
423 => "TN",
865 => "TN",
931 => "TN",
615 => "TN",
901 => "TN",
731 => "TN",
254 => "TX",
325 => "TX",
713 => "TX",
940 => "TX",
817 => "TX",
430 => "TX",
903 => "TX",
806 => "TX",
737 => "TX",
512 => "TX",
361 => "TX",
210 => "TX",
936 => "TX",
409 => "TX",
979 => "TX",
972 => "TX",
469 => "TX",
214 => "TX",
682 => "TX",
832 => "TX",
281 => "TX",
830 => "TX",
956 => "TX",
432 => "TX",
915 => "TX",
435 => "UT",
801 => "UT",
385 => "UT",
434 => "VA",
804 => "VA",
757 => "VA",
703 => "VA",
571 => "VA",
540 => "VA",
276 => "VA",
381 => "VA",
236 => "VA",
802 => "VT",
509 => "WA",
360 => "WA",
564 => "WA",
206 => "WA",
425 => "WA",
253 => "WA",
715 => "WI",
920 => "WI",
414 => "WI",
262 => "WI",
608 => "WI",
353 => "WI",
420 => "WI",
304 => "WV",
307 => "WY");
?>

Integrating reinvigorate name tags into a PHP site

This is a script which will intergrate reinvigorate name tags into your PHP site using cookies
Put this on any page you want to have visitors enter their name
<FORM ACTION="index.php" METHOD="GET">
<p>Name <INPUT TYPE=TEXT NAME= "n" SIZE=20><br>
<INPUT TYPE=SUBMIT VALUE="Submit!">

Put this on your index.php page at the top before anything. This will set a cookie that will last for 2 months
<?php
$Name = $_GET['n'];
?>
<?php
$inTwoMonths = 60 * 60 * 24 * 60 + time(); 
setcookie('name', $Name, $inTwoMonths); 
?>

Put this at the first line after the <body> tag. You need to change the USER-ID to your user ID that
you can get from the regular reinvigorate code.
<script type="text/javascript" src="http://include.reinvigorate.net/re_.js"></script>
<script type="text/javascript">
var re_name_tag = "<?php echo $Name; ?>";
re_("USER-ID");
</script>


« Newer Snippets
Older Snippets »
Showing 1-10 of 20 total  RSS