Copy your clipboard to the pastebin

Klipper Pastie.org Menu When you are a power IRC user, you might know the problem. You cannot copy the whole source code, error message or log file etc. directly in the IRC channel. You need a pastebin. I like http://pastie.org really much. It has a clean interface and supports highlighting for many languages. But how to copy the text to the pastebin in a handy and short way?

Do the following to copy the clipboard content to the pastebin by a simple <Ctrl>+<Alt>+<R> (global shortcut to open Klipper actions) and a click:

  • Copy the file pastie.rb to a folder which is in your $PATH
  • Make the file executable for you
  • Edit the actions of Klipper and add for the .* Regexp (means: no special string) a new action (do not activate automatic).
  • Add the command echo '%s' | pastie.rb, feedback should go to clipboard and set the description to “post as plain text” for instance
  • You can add another command echo '%s' | pastie.rb -f ruby to paste the text with ruby syntax highlighting

Klipper Pastie.org Settings After that you should be able to send your clipboard to the pastebin with one selection (to copy text into clipboard), one hotkey (to trigger Klipper actions) and one click (to choose between different highlighters). You can paste the URL with a single click on the middle button of your mouse. You don’t even have to open the pastebin page yourself!

I like it. Just want to share this with you in the case you was locking for something similiar. :)

pastie.rb

Copy this file to your ~/bin and make it executable.

A big thank to the unknown author of this file. I found it via google on http://pastie.org and did only some minor modifications on it.

You can use the pastie.rb script via command line by pipe a file to it. To set the code highlighting use the switch -f LANG. To get all supported languages you want to try a pastie.rb -h.

#!/usr/bin/env ruby
# kate: remove-trailing-space on; replace-trailing-space-save on; indent-width 2; indent-mode ruby; syntax ruby;
# file: pastie.rb

require 'net/http'
require 'optparse'
require 'timeout'
require 'cgi'
require 'uri'

class Hash

  def to_query_string
    map { |k, v|
      if v.instance_of?(Hash)
        v.map { |sk, sv|
          "#{k}[#{sk}]=#{sv}"
        }.join('&')
      else
        "#{k}=#{v}"
      end
    }.join('&')
  end

end

module Pastie

  AVAILABLE_PARSERS = %w( objective-c++ actionscript ruby ruby_on_rails diff
    plain_text c++ css java javascript html html_rails shell shell-unix-generic
    sql php python pascal perl yaml csharp go apache lua io lisp d erlang fortran
    haskell literate_haskell makefile scala scheme smarty ini nu tex clojure
  )

  class API

    PASTIE_URL = URI.parse "http://pastie.org/pastes"

    def paste(body, format = 'plain_text', is_private = false)
      raise InvalidParser unless valid_parser? format

      http = Net::HTTP.new PASTIE_URL.host, PASTIE_URL.port

      query_string = { :paste => {
        :body => CGI.escape(body),
        :parser => format,
        :restricted => is_private,
        :authorization => 'burger'
      }}.to_query_string

      response, body = http.start do |http|
        http.post PASTIE_URL.path, query_string
      end

      raise Pastie::Error unless response.code == '302'

      response['location']
    end

    private

    def valid_parser?(format)
      Pastie::AVAILABLE_PARSERS.include? format
    end

  end

  class Error < StandardError; end
  class InvalidParser < StandardError; end

  class ConsoleOptions

    attr_reader :parser, :options

    def initialize
      @options = {
        :format => 'plain_text',
        :private => false
      }

      @parser = OptionParser.new do |cmd|
        cmd.banner = "Ruby Pastie CLI - takes paste input from STDIN"

        cmd.separator ''

        cmd.on('-h', '--help', 'Displays this help message') do
          puts @parser
          exit
        end

        cmd.on('-f', '--format FORMAT', "The format of the text being pasted. Available parsers: #{Pastie::AVAILABLE_PARSERS.join(', ')}") do |format|
          @options[:format] = format
        end

        cmd.on('-p', '--private', 'Create a private paste') do
          @options[:private] = true
        end
      end
    end

    def run arguments
      @parser.parse!(arguments)

      body = ''

      Timeout.timeout(1) do
        body += STDIN.read
      end

      if body.strip.empty?
        puts "Please pipe in some content to paste on STDIN."
        exit 1
      end

      pastie = API.new
      puts pastie.paste(body, @options[:format], @options[:private])

      exit 0
    rescue InvalidParser
      puts "Please specify a valid format parser."
      exit 1
    rescue Error
      puts "An unknown error occurred"
      exit 1
    rescue Timeout::Error
      puts "Could not read from STDIN."
      exit 1
    end
  end
end

if ($0 == __FILE__)
  app = Pastie::ConsoleOptions.new
  app.run(ARGV)
end

pastie.rb

Call for Konqueror related GSoC 2010 projects

I really wonder why there are no Konqueror related idea for a GSoC 2010 project in the KDE Community Wiki.

The last opensuse version ships Firefox as default browser, there are some other attempts of a kde based browser (rekonq, etc.), but I think that nothing can replace Konqueror in the next time concerning consumption of resources. Konqueror is so fast using pdf viewer KPart Okular while Firefox almost hangs up with proprietary plugin adobe reader. Which other browser can split its view and show pdf viewer and webpages at the same time? KatePart is also very handy.

I mean it’s wrong to think that other Qt based browsers can replace Konqueror in the next time. Even the inexperienced user wants to use KPart Okular.

However, I use Firefox, because it doesn’t use kthml has awesome bookmark tagging and can be synced with Mozilla Weave. It would be nice to see these features prospectively integrated in Konqueror, too.

What do you think? Can we bring Konqueror to the next level?

KonsolePart

GUI integrating KDE libkonsolepart To get a shell in your GUI, you don’t have to reinvent the wheel - just use the KonsolePart provided by KDE KParts. If you use the KDE environment you already know the libkonsolepart, because Dolphin, Konqueror, Kate, Yakuake, etc. take usage of it.

To get this small demo run, you need a KDE version already containing SVN commit 1085699 (ttanks no Arno, who made the Part accessable within ruby). The opensuse buildservice should provide a package ruby-kde4 in Factory Repo, but I build I didn’t test it.

Just copy both files in the same directory, make the main.rb executable and run it.

The example contains a little bit more than neccessary, because it implements a fully customizable menus/toolbars via KXmlWindow and shows how to use actions and slots.

To get only the KonsolePart widget, you only have to look at the KonsolePart class. The class itself bases on a short code example from Arno. Kudos to him!

For further explanation just take a look at the source code.

Usage

You can change the working directory for the built-in shell by a nice dialog and can execute some command by the lineedit. To send your command to the shell, you have to press enter.

I know there are a lot of bugs. :wink: But it is just a quick and dirty demo.

main.rb

#!/usr/bin/env ruby

$KCODE = 'u'

require 'korundum4'

class KonsolePart

  def initialize parent

    factory = KDE::PluginLoader.new("libkonsolepart").factory
    @part = factory.create("KParts::ReadOnlyPart", parent, nil, [], "")

    @terminal = @part.qobject_cast(KDE::TerminalInterfaceV2)

    if @terminal.nil?
      @terminal = @part.qobject_cast(KDE::TerminalInterface)
    end
    pwd

  end

  public

  def widget
    @part.widget
  end

  # http://api.kde.org/4.3-api/kdebase-apps-apidocs/konsole/html/Part_8h_source.html
  def pwd path = '.'
    @terminal.show_shell_in_dir path
  end

  def cmd input
    @terminal.send_input input
  end

end

class CustomWidget < KDE::XmlGuiWindow

  slots :changedir, :docmd

  def initialize( parent = nil )

    super

    resize( 800, 600 )

    ##### Prepare Central Widget
    @centralwidget = Qt::Widget.new self
    setCentralWidget @centralwidget

    #### Build window layout
    @centralWidgetVLayout = Qt::VBoxLayout.new @centralwidget

    @kPBtnChangePwd = KDE::PushButton.new @centralwidget
    @kPBtnChangePwd.connect( :clicked, self, :changedir)
    @centralWidgetVLayout.addWidget @kPBtnChangePwd
    @kLineEdt = KDE::LineEdit.new @centralwidget
    @kLineEdt.connect( :returnPressed, self, :docmd )
    @centralWidgetVLayout.addWidget @kLineEdt
    @centralWidgetVLayout.add_stretch

    ##### Build Konsole Panel
    @konsoleDockWidget = Qt::DockWidget.new self
    @konsoleDockWidget.objectName = "konsoleDockWidget"
    @konsoleDockWidget.features = Qt::DockWidget::AllDockWidgetFeatures
    @mykonsole = KonsolePart.new @konsoleDockWidget
    @konsoleDockWidget.setWidget @mykonsole.widget
    addDockWidget Qt::BottomDockWidgetArea, @konsoleDockWidget

    ##### Prepare actions
    @closeAction = KDE::StandardAction::quit( self, SLOT( :close ), actionCollection )
    actionCollection.addAction "toggle_konsole", @konsoleDockWidget.toggleViewAction

    ##### Prepare GUI
    setupGUI( Default, File.dirname( File.expand_path( __FILE__ ) ) + "/ui.rc" )
    retranslate_ui

  end

  def changedir
    folder = %x{ kdialog --getexistingdirectory . }
    @mykonsole.cmd "cd #{folder}" unless folder.nil?
  end

  def docmd
    @mykonsole.cmd "#{@kLineEdt.text}\n"
    @kLineEdt.text = ""
  end

  def retranslate_ui
    setWindowTitle i18n "KPlaylistTool"
    @konsoleDockWidget.setWindowTitle i18n "Konsole"
    @kPBtnChangePwd.setText i18n "Change Directory"
  end
end

about = KDE::AboutData.new("konsoleinruby", "konqueror", KDE.ki18n("Konsole in Ruby"), "0.1", KDE::ki18n("A short description"), KDE::AboutData::License_GPL_V3, KDE::ki18n("© 2010 Robert Riemann"), KDE::ki18n("See kde-apps.org for Updates"), "http://homepage.de", "bugs@homepage.de" )
about.setProgramIconName  "plasma"
KDE::CmdLineArgs.init(ARGV, about)
a = KDE::Application.new
w = CustomWidget.new
w.show
a.exec

main.rb

ui.xml

<?xml version="1.0" encoding="UTF-8"?>
<gui name="konsole_in_ruby">
     version="1"
     xmlns="http://www.kde.org/standards/kxmlgui/1.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.kde.org/standards/kxmlgui/1.0
                         http://www.kde.org/standards/kxmlgui/1.0/kxmlgui.xsd" >
  <MenuBar>
    <Menu name="file" >
    </Menu>
    <Menu name="view" >
      <Action name="toggle_konsole" />
    </Menu>
  </MenuBar>
  <ToolBar name="mainToolBar" >
    <text>Main Toolbar</text>
    <Action name="toggle_konsole" />
    <Action name="file_quit" />
  </ToolBar>
</gui>

ui.xml

Simple Ruby Plasmoid

Hello Planet KDE, hello RubyCorner

After some blog posts on my old blog about KDE programming with Ruby I decided to bring the content a little bit more to the people. Here I am.

Two days before yesterday I got a mail concerning a problem related to creating plasmoids with Ruby. I never had the wish to create my own plasmoid. I thought it would be difficult, but while getting a closer look, I’ve noticed how easy it is - in the case of using Ruby.

Simple Ruby Plasmoid

Simple Plasmoid made with Ruby This example is a modified version from the one in the KDE techbase wiki.

The plasmoid sends the content of a QLineEdit to the clipboard when pressing the QPushButton or pressing the enter key and clears the QLineEdit afterwards.

Test It

If you want to try it yourself, you just have to extract simple_ruby_plasmoid_clipboard.tar.gz, change the directory to ruby-test-applet and start the plasmoid in a special viewer with plasmoidviewer.

You have to install the Ruby KDE bindings package (on opensuse it is called ruby-kde4), but on a lot of KDE4 systems this should be installed already.

Understand It

First you need the right directory tree for your plasmoid. It should be look like this:

ruby-test-applet/
|-- contents
|   `-- code
|       `-- main.rb
`-- metadata.desktop

You need at least two files. The first one is main.rb, that contains your program code.

When accessing KDE libs from Ruby you write nearly the same code as you would write in C++.

A short example in C++:

setMinimumSize( 150, 150 )
Plasma::LineEdit line_edit( parent )

The same using Ruby:

set_minimum_size 150, 150
# or
setMinimumSize 150, 150
# or
self.minimum_size = 150, 150
line_edit Plasma::LineEdit.new self

Member variables begins with an @ sign. There are different aliases for KDE and Qt methods. You can omit brackets in a lot of cases. You don’t need any header files. You don’t need to compile.

require 'plasma_applet'

module RubyTestApplet
  class Main < PlasmaScripting::Applet

    slots :addText

    def init
      set_minimum_size 150, 150

      @layout = Qt::GraphicsLinearLayout.new Qt::Vertical, self
      self.layout = @layout

      @label = Plasma::Label.new self
      @label.text = 'This plasmoid will copy the text you enter below to the clipboard.'
      @layout.add_item @label

      @line_edit = Plasma::LineEdit.new self

      begin
        @line_edit.clear_button_shown = true # not supported in early plasma versions
      rescue
        nil # but that doesn't matter
      end

      @layout.add_item @line_edit

      @button = Plasma::PushButton.new self
      @button.text = 'Copy to clipboard'
      @layout.add_item @button

      Qt::Object.connect( @button, SIGNAL(:clicked), self, SLOT(:addText) )
      Qt::Object.connect( @line_edit, SIGNAL(:returnPressed), self, SLOT(:addText) )
    end

    def addText
      Qt::Application.clipboard.text = @line_edit.text
      @line_edit.text = ""
    end

  end
end

# kate: remove_trailing_space on; replace-trailing-space-save on; indent-width 2; indent-mode ruby;

The second file you need is the metadata.desktop containing all the meta data. :wink:

[Desktop Entry]
Name=Simple Ruby applet
Comment=This is a simple applet written in Ruby
Icon=chronometer
Type=Service
ServiceTypes=Plasma/Applet

X-Plasma-API=ruby-script
X-Plasma-MainScript=code/main.rb

X-KDE-PluginInfo-Author=Me
X-KDE-PluginInfo-Email=me@example.com
X-KDE-PluginInfo-Name=ruby-test
X-KDE-PluginInfo-Version=0.1
X-KDE-PluginInfo-Website=http://plasma.kde.org/
X-KDE-PluginInfo-Category=Examples
X-KDE-PluginInfo-Depends=
X-KDE-PluginInfo-License=GPL
X-KDE-PluginInfo-EnabledByDefault=true

metadata.desktop

What do you think? So easy, isn’t it? If you have ever played a little bit with Ruby and Qt or KDE you should know enough to create your own plasmoid within a quarter of an hour!

Just moved to Jekyll

Static Website Generators

After using the static website generator webgen I started to appreciate the idea not to depend on special hosts with php, mysql and so on enabled. You can put all files on a CD and it will works nevertheless. Like in the good old days, when there was only the notepad and the Netscape Navigator.

My last blog was powered by Movable Type. A really good, full-featured engine, but editing in the ajax backend was damn slow, because I hosted Movable Type myself via a dynamic dns service. And at home the provided upload rate is not good enough to host a page serious. I don’t want to pay for websites. There is so much static webspace in the www. So I switched over to the ruby1 driven Jekyll and got in addtion the hosting for free on http://github.com.

Building The Blog

I started with a copy of http://github.com/schacon/schacon.github.com. At this point a big “Horay!” to schacon and open source, that made a quick start possible.

While editing the files I found some usefull tricks you, you maybe googled for.

CSS selector for elements without class attribute

I wanted a template which allows to use nice CSS formatted <code> and <pre> tags for source code, but on the other hand I didn’t want to change the style of included code snippets from http://pastie.org or http://refactormycode.com.

So I found out how to change only these html tags in the source code, which doesn’t have an class attribute, because that is exactly the difference between these pastie-snippets and my own pre-elements.

The solution is to use the negation pseudo-class selector.

pre, code {
  font-family:Consolas,"Andale Mono","Courier New",Courier,mono !important;
  font-weight: 400;
  font-size:0.9em;
}

*:not(PRE) > code {
  background-color: #333;
  color: #fff;
}

pre:not([class]) {
  background-color: #333;
  border: 1px solid #000;
  color: #fff;
  margin: 1em 0;
  overflow: auto;
  padding: 0.5em;
}

First I define some basic font styles for all kind of code. After that I define styles for the code, which isn’t in a pre-element and at last follows the style for the pygments-generated in-built code.

Integrating other static pages

As BSc physics student I got a userpage on the IT system of the physics institute, where I store some files. Furthermore I have an account at DESY, from where I also get a userpage. So I thought it would be nice to integrate both pages.

In the end I got it by defining a special header and footer in the .htaccess file for the Apache auto-generated directory index.

ReadmeName footer.html
HeaderName header.html
IndexStyleSheet style.css
IndexIgnore style.css footer.html header.html

.htaccess

More explanation are on the official Apache Module page given.

You might also want to look in my blog sources.

Generating pygments CSS files

I didn’t found it immediately in the internet, so I want to increase the pages containting the hint by 1.

If you want to create the CSS file, maybe with the look and feel of http://pastie.org, try this in your cmd to generate a complete “highlight.css”.

pygmentize -f html -a .highlight -S pastie > highlight.css

To get an overview over possible you have to execute this command:

pygmentize -L styles

Syntax highlighting is done via pygments, which bases on python. Is that really necessary?

  1. Ruby is the best language I’ve seen so far. So the new system had to be based on it. ↩︎

Pagination