Difference between revisions of "Luakit"

From ArchWiki
Jump to: navigation, search
m (Adblock: Typo)
m (Converting plain text bookmarks to SQLite format)
Line 147: Line 147:
 
Here follow a sample Lua script to do that:
 
Here follow a sample Lua script to do that:
 
{{hc|bookmarks_plain_to_sqlite.lua|<nowiki>
 
{{hc|bookmarks_plain_to_sqlite.lua|<nowiki>
local usage = [[Usage: luakit -c bookmark_migrate.lua [bookmark plaintext path] [bookmark db path]
+
local usage = [[Usage: luakit -c bookmarks_plain_to_sqlite.lua [bookmark plaintext path] [bookmark db path]
 
]]
 
]]
  
Line 202: Line 202:
  
 
As stated at beginning of the script, you must run it with Luakit:
 
As stated at beginning of the script, you must run it with Luakit:
  $ luakit -c bookmark_migrate.lua [bookmark plaintext path] [bookmark db path]
+
  $ luakit -c bookmarks_plain_to_sqlite.lua [bookmark plaintext path] [bookmark db path]
 
Your old plaintext bookmarks will be left unchanged. If the DB bookmarks do not exist, the file will be created. If it exist, do not worry, none of the previous bookmarks will be touched. However, this behaviour implies that you might get some doubles.
 
Your old plaintext bookmarks will be left unchanged. If the DB bookmarks do not exist, the file will be created. If it exist, do not worry, none of the previous bookmarks will be touched. However, this behaviour implies that you might get some doubles.
 
  
 
==== Import from Firefox ====
 
==== Import from Firefox ====

Revision as of 07:19, 4 October 2012

Luakit is an extremely fast, lightweight and flexible web browser using the webkit engine. It is customizable through lua scripts and fully usable with keyboard shortcuts.

Installation

The luakit package can be found in the official repositories and can be installed with pacman.

You can also use the git version available from the [AUR].

With the Unix philosophy in mind, Luakit is entirely customizable through its configuration files. Those files are written in the Lua scripting language, thus allowing virtually unlimited features. First, copy the configuration files to your $XDG_CONFIG_HOME folder:

cp -r $XDG_CONFIG_DIRS/luakit $XDG_CONFIG_HOME

Now you can edit any of these files to make your browser fits your needs. Even if you do not know much about Lua, the configuration is simple and well commented enough to make it straightforward.

Basic usage

Note: Most of the shortcuts are viewable and customizable from binds.lua.

Press Template:Keypress to access the command prompt. You can do nearly everything from there. Use Template:Keypress to autocomplete commands.

To quit, use the quit command, or the Template:KeypressTemplate:Keypress shortcut. You can also close the browser while remembering the session (i.e. restoring the tabs) by using the writequit command instead, or the Template:KeypressTemplate:Keypress shortcut.

Browsing

Input fields

Many webpages have editable elements like dropdown lists, checkboxes, text fields and so on. While they work perfectly with the mouse, you may encounter some troubles using the follow commands. In such a case, pressing the arrow keys may help. Alternatively, you can use the focus input Template:Keypress shortcut.

Bookmarks

If enabled (default configuration), bookmarks can be used from within Luakit.

  • The :bookmarks command opens the bookmarks page. (Shortcut: Template:Keypress or Template:Keypress for new tab).
  • The :bookmark uri group command adds the specified URI to the specified group in the bookmarks.
  • You can auto-complete the uri field by pressing Template:Keypress in command mode.

Configuration

Homepage

Set your homepage as follows:

globals.lua
globals = {
    homepage = "about:blank",
-- ...
}

Custom search engines

You can virtually add any search engine you want. Make a search on the website you want and copy paste the URI to the Luakit configuration by replacing the searched terms with an %s. Example:

globals.lua
search_engines = {
    aur = "https://aur.archlinux.org/packages.php?O=0&K=%s&do_Search=Go",
-- ...
}

The variable is used as a keyword for the :open command in Luakit.

Set the defaut search engine by using this same keyword:

globals.lua
search_engines.default = search_engines.aur

Download location

This can be specified in rc.lua :

-- Add download support                                                                                                                                                                                                                        
require "downloads"
require "downloads_chrome"

-- Set download location                                                                                                                                                                                                                                      
downloads.default_dir = os.getenv("HOME") .. "/downloads"
downloads.add_signal("download-location", function (uri, file)
    if not file or file == "" then
        file = (string.match(uri, "/([^/]+)$")
            or string.match(uri, "^%w+://(.+)")
            or string.gsub(uri, "/", "_")
            or "untitled")
    end
    return downloads.default_dir .. "/" .. file
end)

Adblock

There are several plugins available out there.

The official website features this one: https://github.com/mason-larobina/luakit/wiki/AdBlock-Lua-module

  • Put the adblock.lua and adblock_chrome.lua files in your $XDG_CONFIG_HOME/luakit. (mostly your ~/.config/luakit folder).
  • Edit rc.lua and add
rc.lua
require "adblock"
require "adblock_chrome"
  • Fetch an adblock-compatible list, like Easylist, and save it to $XDG_DATA_HOME/luakit (mostly your ~/.local/share/luakit folder).
  • Restart Luakit to load the extension.
  • Use :adblock-list-enable <number> command within Luakit to turn Adblock's list[s] you downloaded on; AdBlock itself becomes enabled on startup.

Full info on enabled lists and AdBlock state you can find using :adblock or gA at luakit://adblock/ internal page, if you enabled adblock_chrome module which is not a mandatory part.

AdBlock for Luakit plugin is also available as a part of Luakit Plugins project: https://github.com/mason-larobina/luakit-plugins/

Bookmarks management

Sync

Starting from version 2012.09.13, Luakit bookmarks are stored in an SQLite database: $XDG_DATA_HOME/luakit/bookmarks.db.

You can put a symbolic link in place of the default file to store your bookmarks anywhere on your machine. This way if your are using a cloud sync application like Dropbox, you can keep your bookmarks synchronized between your different computers.

Converting plain text bookmarks to SQLite format

Luakit 2012.03.25 featured bookmarks stored in a simple plain text file: $XDG_DATA_HOME/luakit/bookmarks. Each line is a bookmark. It is composed of 2 fields:

$XDG_DATA_HOME/luakit/bookmarks
link    group   
Warning: Each field is followed by a tabulation. If you insert spaces instead, the link will not be properly bookmarked.

Groups and links are alphabetically sorted, so there is no need to do it manually.

If you want to use your bookmarks with the latest Luakit release, you will have to convert the file. Here follow a sample Lua script to do that:

bookmarks_plain_to_sqlite.lua
local usage = [[Usage: luakit -c bookmarks_plain_to_sqlite.lua [bookmark plaintext path] [bookmark db path]
]]

local old_db_path, new_db_path = unpack(uris)

if not old_db_path or not new_db_path then
   io.stdout:write(usage)
   luakit.quit(1)
end

-- One-pass file read into 'data' var.
old_db = assert(io.open(old_db_path, "r"))
local data = old_db:read("*all")
assert(old_db:close())

-- Init new_db, otherwise sqlite queries will fail.
new_db = sqlite3{ filename = new_db_path }
new_db:exec("CREATE TABLE IF NOT EXISTS bookmarks (id INTEGER PRIMARY KEY, uri TEXT NOT NULL, title TEXT NOT NULL, desc TEXT NOT NULL, tags TEXT NOT NULL, created INTEGER, modified INTEGER )")

-- Fill
local url,tag

for line in data:gmatch("[^\n]*\n?") do

   if string.len(line) > 1 then

      print ("["..line.."]")

      -- Get url and tag (if present) from first line.
      _, _, url, tag = string.find(line, "([^\n\t]+)\t*([^\n]*)\n?")

      -- Optional yet convenient output.
      io.write(url)
      io.write("\t")
      io.write(tag)
      io.write("\n")

      -- DB insertion. Nothing will be overwritten. If URL and/or tag already exists, then a double is created.
      new_db:exec("INSERT INTO bookmarks VALUES (NULL, ?, ?, ?, ?, ?, ?)", 
                  {
                     url, "", "", tag or "",
                     os.time(), os.time()
                  })
      end
end

print("Import finished.")
print("\nVacuuming database...")
new_db:exec "VACUUM"
print("Vacuum done.")

luakit.quit(0)

As stated at beginning of the script, you must run it with Luakit:

$ luakit -c bookmarks_plain_to_sqlite.lua [bookmark plaintext path] [bookmark db path]

Your old plaintext bookmarks will be left unchanged. If the DB bookmarks do not exist, the file will be created. If it exist, do not worry, none of the previous bookmarks will be touched. However, this behaviour implies that you might get some doubles.

Import from Firefox

Note: This is for Luakit 2012.03.25 only! For newer version, you can first run this script, then convert the generted plain text bookmarks to the SQLite format as stated above.

To import your bookmarks from Firefox, first you must export them to an HTML file using its bookmarks manager. Now we must convert the XML file to a Luakit format. You can use the following one-line awk command:

$ cat bookmarks.html | awk '
{gsub(/\"/," ")}
/<\/H3>/{FS=">";gsub(/</,">");og=g;g=$(NF-2);FS=" "}
/<DL>/{x++;if(x>= 3)gl[x-3]=g}
/<\/DL>/{x--;if(x==2)g=og"2"}
/HREF/{gsub(/</," ");gsub(/>/," ");if(g!=""){if(og!=g){printf "\n";og=g};printf "%s\t",$4;if(x>=3){for(i=0;i<=x-4;i++){printf "%s-",gl[i]}printf "%s\n",gl[x-3]}else{printf "\n"}}}'

Or if you prefer the more readable script version:

ff2lk.awk

# Notes: 'folders' for Firefox bookmarks mean 'groups' for Luakit.

# Put spaces where it is needed to delimit words properly.
{gsub(/\"/," ")}

# Since the folder name may have spaces, delimiter must be ">" here.
/<\/H3>/ {
    FS=">"
    gsub(/</,">")
    oldgroup=group
    group=$(NF-2)
    FS=" "
}

# Each time a <DL> is encountered, it means we step into a subfolder.
# 'count' is the depth level.
# Base level starts at 2 (Firefox fault).
# 'groupline' is an array of all parent folders.
/<DL>/ {
    count++
    if ( count >= 3 )
        groupline[count-3]=group
}

# On </DL>, we step out.
# If if return to the base level (i.e. not in a folder), then we give 'group' a fake name different
# from 'oldgroup' to make sure a line will be skipped (see below).
/<\/DL>/ {
    count--
    if( count == 2 )
        group=oldgroup"ROOT"
}

# The bookmark name.
# If oldgroup is different than group, (i.e. folder changed) then we skip a line.
# If we are in a folder, then we print the group name, i.e. all parents plus the current folder
# separated by an hyphen.
/HREF/ {
    gsub(/</," ")
    gsub(/>/," ")
    if (group != "")
    {
        if(oldgroup != group)
        {
            printf "\n"
            oldgroup=group
        }
        printf "%s\t",$4
        if ( count >= 3 )
        {
            for ( i=0 ; i <= count-4 ; i++ )
            {printf "%s-" , groupline[i]}
            printf "%s" , groupline[count-3]
        }
        printf "\n"
    }
}

Run it with

$ awk -f ff2lk.awk bookmarks.html >> bookmarks

External links