Archive for the ‘web’ Category

check html templates for spelling errors

Tuesday, November 17th, 2009
my spelling is horribly. it’s quite annoying to update a webpage and getting complaints about some spelling errors. usually all the human language strings are nicely stored in a separate textfile but for smaller projects i often put them directly into html templates. if you have a proper ide it will do the spell checking for you. but if you use some not so sophisticated ide you have to do it yourself or you can use one of the opensource spellers. there are ispell, aspell, myspell and some more. at first i tried to write a shellscript using one of them. then i remembered using one once from python. there is this nice library, pyEnchant. it makes spellchecking really easy. here is a little python script which checks all the html templates. the advantage of this is that it can be implemented as a unittest or something similar, so you will be warned if there is a new spelling error in your project.

the script is quick and dirty and for german. but you should get the idea. from enchant.checker import SpellChecker import os, re, codecs, sys chkr = SpellChecker("de_DE") # patterns remove in this case html and jinja2/ django # code and some special words rmPatterns = [r'<.*?>', r'{%.*?%}', r'{{.*?}}', r'me@norep\.com', u'projectName', u'FooName'] # get a list of directories and subdirectories def listdirs(dirname): dirs = [os.path.join(dirname, f) for f in os.listdir(dirname) / if os.path.isdir(os.path.join(dirname, f))] for d in dirs[:]: dirs += listdirs(d) return dirs for d in listdirs(’templates’): for f in [os.path.join(d, f) for f in os.listdir(d) if re.search(r'\.html$', f)]: # read filedata… as unicode, i always use unicode fd = codecs.open(f, ‘r’, ‘utf-8′) data = fd.read() fd.close() # remove the tags and codes defined in rmPatterns for p in rmPatterns: data = re.sub(p, ”, data) # get error words found = [] chkr.set_text(data) for err in chkr: found.append(err.word) # if errors found, print them if len(found) > 0: print “%s: %s” % (len(found), f) for w in found: print ” : %s -> %s” % (w, ‘, ‘.join(chkr.dict.suggest(w))) the output for one of my projects is: 1: templates/pages/about.html : stösst -> stößt, störst 2: templates/pages/help.html : Registrier -> Registrier-, Registriere, Registriert, Registrieren : Bestätigungs -> Bestätigung, Bestätigungs- 3: templates/pages/legalNotice.html : St -> Set, St., Et, Kt, Sh, SV, Ist, Äst, Ast, Ost, Gst, Sät, So : mail -> Mail, mal, -mail, mail-, mai- : tel -> teil, Gel, Tel., Telex, Teller, Telekom

zine, wordpress killer

Sunday, May 10th, 2009
i think about a new blog. a nontechnical trashblog or something. for the kerbtier blog i’m using wordpress and i’m quite happy with it. lots of useful plugins and quite stable. but the code is somewhat funny and it’s not so easy to adapt/extend it to ones needs. i often had to change code instead of reconfigure or add code and this makes it time consuming to keep it up to date.

i looked for another blog software and found zine. it’s quite similar to wordpress but written in python. i did a test installation and till now everything went perfect. i didn’t test it properly but only the fact that it’s written in python makes me belive in it much more than in wordpress.

jj1 webservice step by step

Friday, January 16th, 2009
we will create a little json-rpc webservice in java using jj1. it’s a step by step tutorial using a fresh install of tomcat and java. if you use an ide it should be easy to adabt it. the webservice will generate an ascii banner from a string. the code to generate the ascii banner exists already and just needs to be made accessible as a webservice.

what you need

you need a default tomcat installation and a jvm (java virtula machine). to test if a jvm is available just type java at the command prompt. if something like java version "1.6.0_07" Java(TM) SE Runtime Environment (build 1.6.0_07-b06) Java HotSpot(TM) Server VM (build 10.0-b23, mixed mode) apears you have one installed. otherwise download one from sun or use your package manager to install one. the easiest way to install tomcat is with your package manager if you use a linux like os. if there is no tomcat support take a look at the tomcat setup page.

directories

i usually create a main folder named after the project. in this folder there is a src folder for the java source, a bin folder for the compiled files and a dist folders for jars and wars. if it’s a webapp there is also a web folder which is the webapp. so for our little example app it looks like: asciiText asciiText/src asciiText/bin asciiText/dist asciiText/web asciiText/web/WEB-INF asciiText/web/WEB-INF/lib

dependencies

we need three jars. at first the jj1 jar. the next jar is the stringtree jar. this is used to encode/ decode json. as next we need the service implementation. save all the jars in the asciiText/web/WEB-INF/lib folder.

create the context object

to access a method via json-rpc we have to expose this method to the web. in jj1 this is done via a context object. this is our first… and only java class. we will create it in a ch/kerbtier/asciitext subfolder of the src folder. the ch/kerbtier/asciiws folders represent the namespace or package where the class lives in. the file should be named AsciiContext and look like: package ch.kerbtier.asciiws; import ch.kerbtier.asciitext.AsciiRenderer; import com.googlecode.jj1.server.JsonRpc; public class AsciiContext{ private AsciiRenderer renderer = new AsciiRenderer(); @JsonRpc public String getText(String input, String font, int size){ return renderer.createAscii(input, font, size); } } thats it. we publish the method getText (with the JsonRpc annotation) as a webservice. internally it just calls the createAscii method of the renderer.

web.xml

a webapplication is configured by a web.xml file which lives in the WEB-INF folder of a webapp. we need the following file: <?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>AsciiText</servlet-name> <servlet-class>com.googlecode.jj1.server.Jj1Servlet</servlet-class> <init-param> <param-name>services</param-name> <param-value>root=ch.kerbtier.asciiws.AsciiContext</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>AsciiText</servlet-name> <url-pattern>/ascii</url-pattern> </servlet-mapping> </web-app> this instantiates a Jj1Servlet and loads an AsciiContext instance as a jj1 context and publishes its methods directly under the url ascii.

build the whole stuff

go to your asciiText directory and type: javac -d bin -classpath web/WEB-INF/lib/asciiGenerator.jar:\
web/WEB-INF/lib/jj1.0.1.jar src/ch/kerbtier/asciiws/AsciiContext.java
this compiles the AsciiContext file and places it into your bin directory into the proper package. with the -classpath option you specify the jar files with the classes inside AsciiContext depends on. on windows you need a ; as delimiter between classpath entries. jar cf dist/asciiws.jar -C bin ch this creates a jar file out of the class… it’s useful if you have lots of classes, with one it’s just habit. cp dist/asciiws.jar web/WEB-INF/lib/ copies the generated jar file into the lib folder of the webapp. jar cf dist/asciiws.war -C web WEB-INF this creates the file asciiws.war. now we just need to deploy it with tomcat. one easy way is to just copy it into the webapps folder. after that the webservice should be accessible trough the url http://localhost:8080/asciiws/ascii