Tuesday, October 9, 2012

Validate XML and DTD

Back to school again, I found that I needed to write some XML and DTD by hand.

The most important aspect of the process is to have a valid XML document as well as a valid DTD. There are perhaps some online tools for this, but none of them are very intuitive.

However, there is a tool named xmllint which does exactly this.

It is included with libxml (package), so there's a great chance you already have it.

Here is an example on how to use it:

editors.dtd

<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT editors (editor)>
<!ELEMENT editor  (title, desc?)>
<!ELEMENT title   (#PCDATA)>
<!ELEMENT desc    (#PCDATA)>

editors.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE editors SYSTEM "editors.dtd">
<editors>
  <editor>
    <title>Vim
    <!--Powerful text editor-->
  </editor>
</editors>

And to validate the DTD and XML, execute the following from your command line:
xmllint --valid editors.xml  --dtdvalid editors.dtd

Note: use can use xmllint to validate XML Schemas just use the above command and replace --dtdvalid with with -schema and provide a schema file instead of the DTD file.

Hope that helps!

Wednesday, August 8, 2012

sed replace example

The following command is probably one of the most useful commands one can use to edit parts of files.

grep -lr "int main()" * | xargs sed -i -e 's/foo/bar/'

This will replace all occurrences of foo with bar in all files recursively if they contain int main(). E.g.

int main() {
    int foo = 0;

    return foo;
}

Will be changed to:

int main() {
    int bar = 0;

    return bar;
}

Sunday, July 8, 2012

iptables redirect port

Sometimes you may want to allow users access certain ports. To do so run the following command:

iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

This will allow all HTTP (80) traffic to be redirected to port 3000.

Friday, July 6, 2012

jQuery single draggable in droppable

jQuery UI is a library that works with jQuery which allows you to easily drag and drop elements.

However, sometimes when you have more draggables and wish to let only one reside inside a droppable, you may need to take actions.

Here is some self explaining code that does just that:

$('.item').draggable({
  opacity: 0.35,
  revert: true,
  snap: true,
  zIndex: 2800
});

$('.box').droppable({
  greedy: true,
  drop: function(e, ui) {
    $(this).droppable('option', 'accept', ui.draggable);
    $(this).append(ui.draggable);
  },
  out: function(e, ui) {
    $(this).droppable('option', 'accept', '.item');
  }
});

Thursday, June 28, 2012

cannot load such file -- zlib

When getting started with Ruby, this error might appear:


ERROR: Loading command: install (LoadError)
cannot load such file -- zlib
ERROR: While executing gem ... (NameError)
uninitialized constant Gem::Commands::InstallCommand


However, it is fairly simple to fix. Run the following commands from your terminal.

rvm pkg install zlib
rvm remove 1.9.3
rvm install 1.9.3

Hope that helps!

Thursday, June 21, 2012

Gnome Shell grid workspaces

By default, Gnome Shell tries to maintain only one workspace including the ones that are in use. This can be a great pain when being used to have some sort of structure on your desktop.

There is however a combination of extensions for Gnome Shell that allows you to configure your good 'ol 3x3 or 6x2 set of workspaces! Here are the links:


When both are installed, rigt click anywhere on the workspace indicator on the bottom right side of the screen and select how many workspaces (vertical and horizontal) you want.

Update:

You may also want to hide the bottom panel. By default this is not an option.
However, there are basically two fixes available.

1. With CSS (the most clean way)

Open, and edit ~/.local/share/gnome-shell/extensions/Bottom_Panel@rmy.pobox.com/stylesheet.css on line 6, change the height to 0px.

All credit goes to Brian McSweeney

2. With JavaScript (the alternative way)

For instructions, have a look at this comment at Ask Ubuntu.

All credit goes to Rasmus.

PS

In both cases, remember to update GNOME Shell (Alt + F2) and execute 'r' (for refresh).

Happy tweaking!

Saturday, June 9, 2012

Log out of basic auth with JavaScript

Ever wanted to log out of a basic authentication session with only JavaScript?

The following code requires jQuery, but it is of course possible to hack something together with plain JavaScript.

$.get(document.URL.replace(/(https?:\/\/)/, '$1logout@'));

Update:
I have figured out that this doesn't work all the time, and it seems unreliable. However, it may be used as a last resort.