donderdag 6 september 2012

File type associations with xdg-open and xdg-mime (for openbox) example


Just so I don't forget, here is how to do it - it's very simple, really:

xdg-mime default xarchiver.desktop application/x-compressed-tar

Why ?


When I clicked on a .tar.gz file in chromium, my desktop environment would hang.

This was caused the fact that chromium calls xdg-open to know which program to use to open the .tar.gz file. xdg-open used "xdg-mime query default application/x-compressed-tar" to figure out what application to use (which was none, because I hadn't configured any default application).

After that, xdg-open doesn't stop. If /etc/debian-version exists, it additionally uses run-mailcap to figure out which program to use. And mailcap was configured to run "less" somewhere down the line, which stops and apparently, stops it's parent processes for some unknown reason when run in this configuration.

So I deleted /etc/debian-version because I don't see the point of having it. We'll see if something what breaks.

How ?

xdg-mime will create the file ~/.local/share/applications/mimeapps.list which then contains:

[Default Applications]
application/x-compressed-tar=xarchiver.desktop

zondag 17 juni 2012

Rudimentary PDF to LaTeX conversion in Linux

I finally got around to trying a rudimentary PDF to LaTeX conversion in Linux.

"It's like turning a hamburger into a cow" :-)

Usage:

./pdftolatex.sh "filename.pdf"

Example output:

# pdftolatex.sh test.pdf 

PDF to LaTeX conversion script
Copyleft 2012 (c) Tom Van Braeckel <tomvanbraeckel@gmail.com>

WARNING: this is a rudimentary first stab. Proceed with caution.

Checking if all dependencies are found...
/usr/bin/pdftohtml
Dependency pdftohtml found.
/usr/bin/gnuhtml2latex
Dependency gnuhtml2latex found.
/usr/bin/pdflatex
Dependency pdflatex found.

Converting test.pdf to test.pdfs.html
Page-1
Page-2
Page-3
Page-4
Page-5
Page-6
Page-7
Page-8
Page-9
Page-10
Page-11

Fixing up test.pdfs.html to test.pdf_fixedup.html...

Readying test.pdf_fixedup.html for tex conversion in test.pdf_fixedup_ready_for_tex_conversion.html

Converting test.pdf_fixedup_ready_for_tex_conversion.html to test.pdf_frompdf.tex
The resulting file is in test.pdf_frompdf.tex

Fixing up test.pdf_frompdf.tex to test.pdf_frompdf_fixedup.tex

Converting test.pdf_frompdf_fixedup.tex to test.pdf_frompdf_fixedup.pdf for inspection...

Opening test.pdf_frompdf_fixedup.pdf with Evince - you can try another PDF viewer if you like...

Script source code:

#!/bin/sh
file="$1"
dependencies="pdftohtml gnuhtml2latex pdflatex"
echo "PDF to LaTeX conversion script"
echo "Copyleft 2012 (c) Tom Van Braeckel <tomvanbraeckel@gmail.com>"
echo
echo "WARNING: this is a rudimentary first stab. Proceed with caution."
echo
if [ -z "$file" ]; then
echo "Usage: $0 <pdf file>"
echo "The resulting .tex file will be stored somewhere here."
exit 1
fi
echo

echo "Checking if all dependencies are found..."
for dependency in $dependencies; do
which $dependency
if [ $? -ne 0 ]; then
echo "Dependency $dependency not found, install it using:"
echo "sudo apt-get install $dependency"
exit 1
else
echo "Dependency $dependency found."
fi
done
echo

echo "Converting $file to ${file}s.html"
pdftohtml -nomerge "$file" "$file".html
echo

echo "Fixing up ${file}s.html to ${file}_fixedup.html..."
# This nasty br in a b causes problems later on
sed "s,<br/></b>,</b><br/>,g" "${file}s.html" > "${file}_fixedup.html"
# ending with bold text menas it is the end of a title and can be on a newline
sed -i "s,</b><br/>\$,</b><br/><br/>,g" "${file}_fixedup.html"
# starting with bold text means it is the start of a title so can be on a new line
sed -i "s,^<b>,<br/><b>,g" "${file}_fixedup.html"
# spaces ?
sed -i "s,\&#160;, ,g" "${file}_fixedup.html"
# there is no use in a space before a newline, and it causes a bogus indent when converting to .tex later on
sed -i "s, <br/>,<br/>,g" "${file}_fixedup.html"
# encoding, although gnuhtml2latex ignores this
sed -i "s,<HEAD>,<HEAD><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />," "${file}_fixedup.html"
echo

echo "Readying ${file}_fixedup.html for tex conversion in ${file}_fixedup_ready_for_tex_conversion.html"
cp "${file}_fixedup.html" "${file}_fixedup_ready_for_tex_conversion.html"
# br is ignored and should be replaced by a newline
sed -i "s,<br/>,<p></p>,g" "${file}_fixedup_ready_for_tex_conversion.html"
# Remove bogus links - this fixes the empty } problem
sed -i "s/<A name=[0-9]\+><\/a>//g" "${file}_fixedup_ready_for_tex_conversion.html"
echo

echo "Converting ${file}_fixedup_ready_for_tex_conversion.html to ${file}_frompdf.tex"
# -c = table of contents
# -s = write to standard out
# -p  Break page after title / table of contents
# -H  use hyperref package to process anchors
# -g images
# -n  Use numbered sections
gnuhtml2latex -c -s -p -H -n "${file}_fixedup_ready_for_tex_conversion.html" > "$file"_frompdf.tex
echo "The resulting file is in ${file}_frompdf.tex"
echo

echo "Fixing up ${file}_frompdf.tex to ${file}_frompdf_fixedup.tex"
sed -i 's/\\par/\\newline/g' "${file}_frompdf.tex"
( cat header.inc ; tail -n +7 "${file}_frompdf.tex" ) > "${file}_frompdf_fixedup.tex"
echo

echo "Converting ${file}_frompdf_fixedup.tex to ${file}_frompdf_fixedup.pdf for inspection..."
pdflatex -interaction nonstopmode "${file}_frompdf_fixedup.tex" > tex_to_pdf_errors_and_warnings.txt
echo

echo "Opening ${file}_frompdf_fixedup.pdf with Evince - you can try another PDF viewer if you like..."
evince "$file"_frompdf_fixedup.pdf

The script uses one extra file, header.inc, which contains customizations:

\documentclass[a4paper,11pt,oneside]{article}
\usepackage{a4wide}                     % Iets meer tekst op een bladzijde
\usepackage[dutch]{babel}               % Voor nederlandstalige hyphenatie (woordsplitsing) en het euro-symbool
\usepackage{amsmath}                    % Uitgebreide wiskundige mogelijkheden
\usepackage{amssymb}                    % Voor speciale symbolen zoals de verzameling Z, R...
\usepackage{url}                        % Om url's te verwerken
\usepackage{graphicx}                   % Om figuren te kunnen verwerken
\usepackage[small,bf,hang]{caption}    % Om de captions wat te verbeteren
\usepackage{xspace}                     % Magische spaties na een commando
\usepackage[utf8]{inputenc}           % Om niet ascii karakters rechtstreeks te kunnen typen
\usepackage{float}                      % Om nieuwe float environments aan te maken. Ook optie H!
\usepackage{flafter}                    % Opdat floats niet zouden voorsteken
\usepackage{listings}                   % Voor het weergeven van letterlijke text en codelistings
\usepackage{marvosym}                   % Om het euro symbool te krijgen
\usepackage{eurosym}                   % Om het euro symbool te krijgen
\usepackage{textcomp}                   % Voor onder andere graden celsius
\usepackage{fancyhdr}                   % Voor fancy headers en footers.
\usepackage{graphics}                   % Om figuren te verwerken.
\usepackage[a4paper,plainpages=false]{hyperref}    % Om hyperlinks te hebben in het pdfdocument.
\usepackage[usenames,dvipsnames]{xcolor}

% Definitie algemene macro's
\newcommand{\npar}{\par \vspace{0.2ex }}

\setlength\textheight{9.75in}
\setlength\textwidth{7in}

\topmargin -0.5in 
\headheight 0.0in
\oddsidemargin -.25in 


[Update] You can also try the following method, using abiword, but the method above yields better results, in my opinion:

abiword --to=tex "filename.pdf"

zaterdag 5 mei 2012

Please don't blame the weather

Please don't blame the weather
It’s got nothing to do with the weater.

Blame me and sincerely blame yourself.


Try waking up in the morning and
feeling better than the day before.


Wash off your guilt, get over yourself,
and stop wasting time persuing shit.


Contemplate life’s meaning and
the sadness of existence.

donderdag 1 maart 2012

Keycodes and coffeeshops in the Linux kernel

We are using gpio-keys to configure 2 GPIO's coming from 2 sensors (left and right on our device) as keyboard interrupt lines.

So I had to choose 2 obscure keycodes to map on "proximity meter left" and "proximity meter right"...

Here are the results:

        {
                .gpio           = INT_PROX_L,
                .code           = KEY_COFFEE,
                .desc           = "Proximity Left",
        },
        {
                .gpio           = INT_PROX_R,
                .code           = KEY_SHOP,
                .desc           = "Proximity Right",
        },

These keynames and their order are easy to memorize now, because (since we read from left to right) we know that coffee = left and shop = right :-)

zondag 8 januari 2012

rsync and scp autocompletion

Apparantly, rsync and scp boast tab autocompletion !

When you have configured passwordless SSH access (by copying over your public key) and you use these tools, tab autocompletion works as expected.

For example, typing the following on a bash shell:

rsync localfile.txt exampleserver:/examp<TAB>

will automatically autocomplete the foldername.
In the example's case, it becomes:


rsync localfile.txt exampleserver:/examplefolder/


The same goes for scp, like:


scp localfile.txt exampleserver:/examp<TAB>


becomes


rsync localfile.txt exampleserver:/examplefolder


rsync and scp are able to do this because the passwordless SSH allows them to see what files can be found on the other server without asking for a password.

On my LAN network, it takes about 1 second to autocomplete, which is perfectly acceptable for me. It certainly beats having to open a seperate SSH session and manually making a lising ;-)

Super-sleek shell script as a simple touchscreen controller for Linux

#!/bin/sh

# Beautifully simple shell script to detect screen touches

# I use it to toggle the music player on my FriendlyARM 2440 Mini
# but it can be used for any Linux touch screen


# Copyleft 2011 - t-Omicr0n

while true; do
     head -c 1 /dev/input/event0
     echo "You touched it !"
done &

Hunting advice

Let's say you're a hunter. If a deer takes your gun, shoots itself and then straps itself to the roof of your car..... you have to take it home and eat it ! -- Two and a Half Men