Skip to content

Commit

Permalink
- Performance improvements. (from ~21ms to ~14ms)
Browse files Browse the repository at this point in the history
- Improved the encoding of XML special characters.
- The underscore character (`_`) is now escaped correctly.
- Added XML <![CDATA[]]> for the execution command, which allows any exotic characters to exist inside a given command.
  • Loading branch information
trizen committed Jun 2, 2015
1 parent ed9da46 commit 0984b3c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 51 deletions.
60 changes: 30 additions & 30 deletions obmenu-generator
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,20 @@
# Program: obmenu-generator
# License: GPLv3
# Created on: 25 March 2011
# Latest edit on: 02 April 2015
# Latest edit on: 02 June 2015
# Website: https://github.com/trizen/obmenu-generator

#use 5.014;
#use strict;
#use warnings;

require Linux::DesktopFiles;

$Linux::DesktopFiles::VERSION >= 0.09
|| die "Update Linux::DesktopFiles to a newer version! (requires >=0.09)\n";
require Linux::DesktopFiles; # >= 0.09

my $pkgname = 'obmenu-generator';
my $version = 0.63;
my $version = 0.64;

our ($CONFIG, $SCHEMA);
my $output_h = *STDOUT;
my $output_h = \*STDOUT;

my ($pipe, $static, $icons, $reconfigure, $update_config, $reconf_openbox);

Expand Down Expand Up @@ -430,7 +427,6 @@ STATIC_MENU_HEADER
my %cache;

sub check_icon {
$_[0] // return '';
$cache{$_[0]} //= ((chr ord($_[0]) eq '/') ? $_[0] : $desk_obj->get_icon_path($_[0])
|| $desk_obj->get_icon_path($CONFIG{missing_icon}));
}
Expand Down Expand Up @@ -476,6 +472,12 @@ foreach my $file ($desk_obj->get_desktop_files) {
}
}

my %entities = (
'&' => '&amp;',
'"' => '&quot;',
'_' => '__',
);

foreach my $schema (@$SCHEMA) {
if (exists $schema->{cat}) {
exists($categories{my $category = lc($schema->{cat}[0]) =~ tr/_a-z0-9/_/cr}) || next;
Expand All @@ -491,26 +493,24 @@ foreach my $schema (@$SCHEMA) {
my $exec = $_->{Exec};

foreach my $key (@{$CONFIG{name_keys}}) {
$_->{$key} eq "" or do {
if ($_->{$key}) {
$name = $_->{$key};
last;
};
}
}

foreach my $item ($name, $exec) {
$item =~ tr/"&// && do {
$item =~ s/&/&amp;/g;
$item =~ s/"/&quot;/g;
};
}
# encode the entities
$name =~ tr/"&_//
&& $name =~ s/([&"_])/$entities{$1}/g;

$icons
? <<"ITEM_WITH_ICON"
<item label="$name" icon="${\check_icon($_->{Icon})}"><action name="Execute"><execute>$exec</execute></action></item>
<item label="$name" icon="${\check_icon($_->{Icon})}"><action name="Execute"><execute><![CDATA[$exec]]></execute></action></item>
ITEM_WITH_ICON
: <<"ITEM";
<item label="$name"><action name="Execute"><execute>$exec</execute></action></item>
<item label="$name"><action name="Execute"><execute><![CDATA[$exec]]></execute></action></item>
ITEM

} @{$categories{$category}}
)
)
Expand All @@ -520,10 +520,10 @@ ITEM
my ($command, $label, $icon) = @{$schema->{item}};
$generated_menu .= $icons
? <<"ITEM_WITH_ICON"
<item label="$label" icon="${\check_icon($icon)}"><action name="Execute"><execute>$command</execute></action></item>
<item label="$label" icon="${\check_icon($icon)}"><action name="Execute"><execute><![CDATA[$command]]></execute></action></item>
ITEM_WITH_ICON
: <<"ITEM";
<item label="$label"><action name="Execute"><execute>$command</execute></action></item>
<item label="$label"><action name="Execute"><execute><![CDATA[$command]]></execute></action></item>
ITEM
}
elsif (exists $schema->{sep}) {
Expand Down Expand Up @@ -576,19 +576,19 @@ RECONFIGURE
EOL

$generated_menu .= <<"CONFIG_MENU";
<item label="Configure autostarted apps"><action name="Execute"><execute>$CONFIG{editor} $openbox_conf/autostart</execute></action></item>
<item label="Edit rc.xml"><action name="Execute"><execute>$CONFIG{editor} $openbox_conf/rc.xml</execute></action></item>
<item label="Configure autostarted apps"><action name="Execute"><execute><![CDATA[$CONFIG{editor} $openbox_conf/autostart]]></execute></action></item>
<item label="Edit rc.xml"><action name="Execute"><execute><![CDATA[$CONFIG{editor} $openbox_conf/rc.xml]]></execute></action></item>
<separator />
<item label="Generate a pipe menu"><action name="Execute"><execute>$0 -p</execute></action></item>
<item label="Generate a static menu"><action name="Execute"><execute>$0 -s</execute></action></item>
<item label="Generate a pipe menu with icons"><action name="Execute"><execute>$0 -p -i</execute></action></item>
<item label="Generate a static menu with icons"><action name="Execute"><execute>$0 -s -i</execute></action></item>
<item label="Generate a pipe menu"><action name="Execute"><execute><![CDATA[$0 -p]]></execute></action></item>
<item label="Generate a static menu"><action name="Execute"><execute><![CDATA[$0 -s]]></execute></action></item>
<item label="Generate a pipe menu with icons"><action name="Execute"><execute><![CDATA[$0 -p -i]]></execute></action></item>
<item label="Generate a static menu with icons"><action name="Execute"><execute><![CDATA[$0 -s -i]]></execute></action></item>
<separator />
<item label="Edit menu.xml"><action name="Execute"><execute>$CONFIG{editor} $menufile</execute></action></item>
<item label="Edit the schema file"><action name="Execute"><execute>$CONFIG{editor} $schema_file</execute></action></item>
<item label="Edit the configuration file"><action name="Execute"><execute>$CONFIG{editor} $config_file</execute></action></item>
<item label="Edit menu.xml"><action name="Execute"><execute><![CDATA[$CONFIG{editor} $menufile]]></execute></action></item>
<item label="Edit the schema file"><action name="Execute"><execute><![CDATA[$CONFIG{editor} $schema_file]]></execute></action></item>
<item label="Edit the configuration file"><action name="Execute"><execute><![CDATA[$CONFIG{editor} $config_file]]></execute></action></item>
<separator />
<item label="Regenerate configuration file"><action name="Execute"><execute>$0 -r</execute></action></item>
<item label="Regenerate configuration file"><action name="Execute"><execute><![CDATA[$0 -r]]></execute></action></item>
</menu>
CONFIG_MENU
}
Expand Down
39 changes: 18 additions & 21 deletions schema.pl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
my $editor = $CONFIG->{editor};

our $SCHEMA = [

# COMMAND LABEL ICON
{item => ['xdg-open .', 'File Manager', 'file-manager']},
{item => ['xterm', 'Terminal', 'terminal']},
{item => ['xdg-open http://', 'Web Browser', 'web-browser']},
{item => ['gmrun', 'Run command', 'system-run']},
{item => ['pidgin', 'Instant messaging', 'system-users']},

{sep => 'Applications'},
{sep => 'Categories'},

# NAME LABEL ICON
{cat => ['utility', 'Accessories', 'applications-utilities']},
Expand Down Expand Up @@ -73,38 +73,32 @@
{begin_cat => ['Advanced Settings', 'gnome-settings']},

# Configuration files
{item => ["$editor ~/.conkyrc", 'Conky RC', $editor]},
{item => ["$editor ~/.config/tint2/tint2rc", 'Tint2 Panel', $editor]},
{item => ["$editor ~/.conkyrc", 'Conky RC', 'text-x-source']},
{item => ["$editor ~/.config/tint2/tint2rc", 'Tint2 Panel', 'text-x-source']},

# obmenu-generator category
{begin_cat => ['Obmenu-Generator', 'menu-editor']},
{item => ["$editor ~/.config/obmenu-generator/schema.pl", 'Menu Schema', $editor]},
{item => ["$editor ~/.config/obmenu-generator/config.pl", 'Menu Config', $editor]},
{item => ["$editor ~/.config/obmenu-generator/schema.pl", 'Menu Schema', 'text-x-source']},
{item => ["$editor ~/.config/obmenu-generator/config.pl", 'Menu Config', 'text-x-source']},

{sep => undef},
{item => ['obmenu-generator -p', 'Generate a pipe menu', 'menu-editor']},
{item => ['obmenu-generator -s', 'Generate a static menu', 'menu-editor']},
{item => ['obmenu-generator -p -i', 'Generate a pipe menu with icons', 'menu-editor']},
{item => ['obmenu-generator -s -i', 'Generate a static menu with icons', 'menu-editor']},
{item => ["$0 -p", 'Generate a pipe menu', 'menu-editor']},
{item => ["$0 -s -c", 'Generate a static menu', 'menu-editor']},
{item => ["$0 -p -i", 'Generate a pipe menu with icons', 'menu-editor']},
{item => ["$0 -s -i -c", 'Generate a static menu with icons', 'menu-editor']},
{sep => undef},

{item => ['obmenu-generator -d', 'Refresh Icon Set', 'gtk-refresh']},
{item => ['obmenu-generator -d', 'Refresh Icon Set', 'gtk-refresh']},
{end_cat => undef},

# Openbox category
{begin_cat => ['Openbox', 'openbox']},
{item => ['openbox --reconfigure', 'Reconfigure Openbox', 'openbox']},
{item => ["$editor ~/.config/openbox/autostart", 'Openbox Autostart', $editor]},
{item => ["$editor ~/.config/openbox/rc.xml", 'Openbox RC', $editor]},
{item => ["$editor ~/.config/openbox/menu.xml", 'Openbox Menu', $editor]},
{item => ['openbox --reconfigure', 'Reconfigure Openbox', 'openbox']},
{item => ["$editor ~/.config/openbox/autostart", 'Openbox Autostart', 'shellscript']},
{item => ["$editor ~/.config/openbox/rc.xml", 'Openbox RC', 'text-xml']},
{item => ["$editor ~/.config/openbox/menu.xml", 'Openbox Menu', 'text-xml']},
{end_cat => undef},

# Pacman category
{begin_cat => ['Pacman / Servers', 'package-manager-icon']},
{item => ["gksu $editor /etc/makepkg.conf", 'Makepkg Config', 'start-here']},
{item => ["gksu $editor /etc/pacman.conf", 'Pacman Config', 'start-here']},
{item => ["gksu $editor /etc/pacman.d/mirrorlist", 'Pacman Mirrorlist', 'start-here']},
{end_cat => undef},
{end_cat => undef},
{sep => undef},

Expand All @@ -113,4 +107,7 @@

# This option uses the default Openbox's action "Exit"
{exit => ['Exit', 'exit']},

# This uses the 'oblogout' menu
# {item => ['oblogout', 'Exit', 'exit']},
]

0 comments on commit 0984b3c

Please sign in to comment.