preload
Mai 10
Situation

You want to operate on a linux, unix or mac system some files in a bash script and do some file operations like modifying the text content or moving the files to another location depending on the filename, content or another mathinc pattern.

Problem

Wondered why you have problems with windows files in a network when looping over them? Linux takes whitespaces in file name as line separator so that echoing the file will cause a separate line and so you cannot operate on this file.

Solution

I guess it is called Instruction Field Separator or something. You have to set this only to line break and not to whitespaces. First save the old variable content, then set the new content so that the bash do not takes whitespaces as line separators

1
2
3
4
5
6
7
8
9
OLDIFS=$IFS
IFS=$(echo -en "\n\b")
 
for FILE in $(find . -name '*.txt)
do
   echo $FILE
done
 
$IFS = $OLDIFS
Tagged with:
Apr 01

Why jquery or why use a js-lib at all?

There are many Javascript libraries on the market, e.g. script.aculo.us, jquery, dojo, Prototype and so on. For most of the tasks there is no special reason why jquery. It ist mostly a matter of actual knowledge of the developers to keep learning time low when there is a basic knowledge available. Some JS-libs have an easier handling e.g. of json than other, in detail using json in jquery is simpler than in prototype.

Why should you use a js-lib at all? There are several reasons:

Simpler syntax and less code

Imagine you want to modify every div-element which has class “payment” assigned and want to set the background color to “#cccccc” to these elements.
You have something like this:

1
2
3
4
5
6
<div id="list">
  <div class="payment">some text</div>
  <div>some text</div>
  <div class="payment">some text</div>
  <div class="payment">some text</div>
</div>

With normal JS you could do the job like this:

?View Code JAVASCRIPT
1
2
3
4
5
6
var list = document.getElementById('list');
for (var i = 0; i &lt; list.childNodes.length; i++) {
    if (list.childNodes.item(i).className == "payment") {
    	list.childNodes.item(i).style.backgroundColor = '#cccccc';
    }
}

Ok, six lines of code and moderate readable but what with all browser support? Umpf. And imagine this is a simple example.

With jquery you do:

?View Code JAVASCRIPT
1
$('div.payment').css('background-color' , '#cccccc');

And this runs in all browsers.

Better readability

Isn’t the example above better readable? You use css-selectors for selecting elements in javascript.

E.g. you want to remove the class from the div elements which are childs of the div with id “list”.

?View Code JAVASCRIPT
1
2
3
$('div#list div.payment').each(function() {
	$(this).removeClass('payment');
})

Better and easier extendability

You can simply create own plugins.

Multi-Browser compatibility

Nearly all functions will run in every browser.

There are often some cons why you should not use js-libs:

Big libray included

Yes it can be but e.g. with jquery you can use the compressed version for the live version. This will have only round about ~50k. And browser will cache the js file.

Higher loading times

Travering can be a costy thing, true. But with newer version it will become faster.

Conclusion

So I recommend using jquery because of the better readability, extendability and the separation of concerns.

Tip of the day:

Do not put JS-code inside html document when not needed. Use event-handling in outsourced js-file.

More infos about jquery you can find under http://www.javascriptkata.com/2007/05/29/3-reasons-why-i-use-jquery/

Tagged with:
Mrz 30

To fix some menu entries in your menu so that they are displayed all the time use the following sample code:

?View Code JAVASCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
lib.mainMenu.alwaysActivePIDlist = 3,4,5
#  main menu
lib.mainMenu= HMENU
lib.mainMenu.entryLevel = 0
#  show these uids of sites no matter where we are lib.mainMenu.alwaysActivePIDlist  = 3,4,5 lib.mainMenu {
# erstes level
1 = TMENU
1.wrap = &lt;ul  id="topnavigation"&gt;|&lt;/ul&gt;
1{
  # no state: normale  Formatierung
  NO{
    wrapItemAndSub =  &lt;li&gt;|&lt;/li&gt;
  }
  # act state: gültig von der rootseite  bis zur aktuellen Seite
  ACT=1
  ACT{
    wrapItemAndSub = &lt;li  class="menu-level1-active"&gt;|&lt;/li&gt;
  }
  # cur state: gültig für die aktuelle  Seite
  CUR=1
  CUR{
    wrapItemAndSub = &lt;li  class="menu-level1-current-active"&gt;|&lt;/li&gt;
  }
  # ifsub state: gültig für seiten die  unterseiten haben
  IFSUB=1
  IFSUB{
    wrapItemAndSub = &lt;li  class="menu-level1-with-subpage"&gt;|&lt;/li&gt;
  }
}
# zweites level
2 = TMENU
2.wrap = &lt;ul  class="menu-level2"&gt;|&lt;/ul&gt;
2{
  NO{
    wrapItemAndSub =  &lt;li&gt;|&lt;/li&gt;
  }
  ACT=1
  ACT{
  wrapItemAndSub = &lt;li  class="menu-level2-active"&gt;|&lt;/li&gt;
  }
  CUR=1
  CUR{
    wrapItemAndSub = &lt;li  class="menu-level2-current-active"&gt;|&lt;/li&gt;
  }
  IFSUB=1
  IFSUB{
    wrapItemAndSub = &lt;li  class="menu-level2-with-subpage"&gt;|&lt;/li&gt;
  }
}
Tagged with:
Mrz 30

Found an interesting article about Data URIs.
It is possible to embed the image data directly into the document. This can either be the html document or a css-file.

1
2
3
4
5
6
7
li {
  background:
    url(data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7)
    no-repeat
    left center;
  padding: 5px 0 5px 25px;
}

This is cool stuff but will not run in IE<8.

So why should i use this? Normally not, but to save requests.

Great article from http://css-tricks.com/data-uris/

Tagged with:
Mrz 25

Sometimes you have to revert changes in svn from lets say a commit you made some days ago. Since then more commits where done and this can be a hard job to undo your changes. So how to undo the changes e.g. from your change in revision 120 when the actual revision is 140?

Go to your trunk workdir and use the following:

1
mvn merge -r 120:119 http://my.svn.repo/trunk/

The changes you made will be reverted. If there are some conflicts they will be shown.
Maybe it is a good idea to make a dry run via:

1
mvn merge --dry-run -r 120:119 http://my.svn.repo/trunk/

So no changes are really made but only shows them.

Of course you can use this for reverting merges.

Tagged with:
Mrz 02

Due to the fact I got in touch with maven2 and I think that maven2 is very helpful will give a short introduction. Bigger projects normally have a more complex configuration but I will start from the scratch.

Maven will create a kind of project sceleton with the following command

1
mvn archetype:create -DgroupId=info.sobek.testapp -DartifactId=testapp

If you work with e.g. eclipse, you can create the project files for eclipse:

1
mvn eclipse:eclipse

Now you can import it into eclipse with “import into existing workspace” option of eclipse.

Maven will normally copy the created so called “artifacts” into your local maven repository. Normally located under your home dir and then .m2

When you program depends on several jars and these dependencies are defined in the pom.xml, then maven will copy the jars in their specific version into the local maven repository. This is very helpful because you can simply build your project with another newer or older version of your dependent jar-file.

How to create multi-module projects will be explained in another episode.

Tagged with:
Feb 08

Imagine you have a pdf-file you want to make ocr-recognition. Take a scenario where you want to automatically let your linux pc do the job, e.g. in a folder.

I choose tesseract-ocr as ocr-programm. Easy to install and use and ok for my use. Unfortunately it takes only tif as input file type so that we have to convert the pdf to tif first.

To create a tif file with Ghostscript from pdf:

1
gs -q -r300 -dBATCH -dNOPAUSE -sDEVICE=tiff24nc  -sOutputFile=Dokument2.tif Dokument1.pdf


Now start OCR-recognition with tesseract-ocr (maybe you have to install it).

1
 tesseract Dokument2.tif doc.txt -l deu

-l deu means “Deutsch” for German language recognition.
-r300 means 300 DPI

Now it is easy to create a script which will automatically check a folder for new files and start ocr etc.

E.g. you can create a shell-script and start this shell script via cronjob. Maybe I will write an example here later.

Tagged with:
Jan 16

If you want to printout or save info from manpages simply type:

1
man -t man | ps2pdf - > man.pdf
Tagged with:
Dez 28

There is no vertical-aligment css-attribute in css 2.0. So how to align elements vertically?

1
2
3
4
5
6
7
8
DIV.container {
    min-height: 10em;
    display: table-cell;
    vertical-align: middle }
...
<DIV class="container">
  <P>This small paragraph...
</DIV>

To align vertically the paragraph P on line 7, you have to use display: table-cell; and vertical-align: middle;

Runs on Mozilla, Safari and IE8 but not IE7

This example is from W3.org. Orginal article can be found here.

Tagged with:
Dez 20

If you want to download a file with curl type the following code into bash:

1
curl -O http://www.url.de/file.html

-O means to output to a file and not to stdout, normally the screen.

What if you need to resume a download of a big file?

1
curl -C - -O http://www.url.de/file.html

With -C – you continue downloading the current file. If is important to write -C -. Don’t forget the -

You can also set a referrer and the user agent.

1
curl -C - -O -A http://www.url.de -e "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; de; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6" http://www.url.de/file.html

With -A you can set the referrer and -e sets the user agent.

Pretty helpful.

Tagged with: