http://www.perlmonks.org?node_id=1001161


in reply to Re: Regex help/ Lua parse
in thread Regex help/ Lua parse

I hope that it is not in bad taste to reply twice to your response. If so please forgive me.

After reading your response I read all the linked material and reviewed the code that you provided. Once I was sure I understood your code, I modified my own. Then, taking your comments into consideration, I wrote a new (similar) piece to parse Awesome's theme configuration file. I tried to avoid capturing any data that wouldn't be subsequently referred to. I made sure to read the file only once, and instead assigned the info to related hashes of the form:

 variable_name => value

I provided my code below, first to let you know that the time spent on your response was not in vain, and also in hopes that you would let me know of any complicated/unreadable methods, and/or style/convention infractions before I move on to adding some more interesting functionality. This is some of the most interesting and difficult code I've written thus far in my learning and I plan on expanding it, but I want to keep it clean.

I've been nose deep in Friedl's Mastering Regular Expressions, and it's proven to be extremely useful. I've found regexps to be easier and more powerful, for my purposes, than the Modules that I referred to above. As a result, the regexps in this code are rather long, but I hope still readable.

I've provided my code followed by a larger chunk of the awesome configuration file below:


#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %theme_table; my %taglist_table; my %menu_table; sub buildTables { while (<>) { if (/^\s*(theme\.(?:bg|fg|border|font)(?:\_(?:normal|focus|urg +ent|minimize|marked|width))?)\s*=\s*\"([^"]*)\"\s*$/) { $theme_table{$1} = $2 } if (/^\s*(theme\.taglist\_(?:bg|fg)\_(?:normal|focus|urgent))\ +s*=\s*\"([^"]*)\"\s*$/) { $taglist_table{$1} = $2 } if (/^\s*(theme\.menu\_(?:bg|fg|border|height|width)(?:\_(?:no +rmal|focus|color|width))?)\s*=\s*\"([^"]*)\"\s*$/) { $menu_table{$1} = $2 } } #print Dumper \%theme_table; #print Dumper \%taglist_table; #print Dumper \%menu_table; } sub printSettings { print "=" x 32, "\n"; print " AWESOME CONFIGURATION SETTINGS \n"; print "=" x 32, "\n\n"; foreach my $table (@_) { foreach my $key (sort keys %$table) { print " $key => $tabl +e->{$key}\n" } print "\n"; } } buildTables; printSettings(\%theme_table, \%taglist_table, \%menu_table);

--------------------------- -- Default awesome theme -- --------------------------- theme = {} theme.font = "sans-serif 8: bold" --default bg_normal = #222222 theme.bg_normal = "#000000" --default bg_focus = #535d6c theme.bg_focus = "#000000" theme.bg_urgent = "#ffffff" theme.bg_minimize = "#ff0000" --default fg_normal = #aaaaaa theme.fg_normal = "#ffffff" --default fg_focus = #ffffff theme.fg_focus = "#55B043" theme.fg_urgent = "#ffffff" theme.fg_minimize = "#ffffff" theme.border_width = "2" theme.border_normal = "#000000" --default border_focus = #535d6c theme.border_focus = "#55B043" theme.border_marked = "#91231c" -- There are other variable sets -- overriding the default one when -- defined, the sets are: -- [taglist|tasklist]_[bg|fg]_[focus|urgent] -- titlebar_[bg|fg]_[normal|focus] -- tooltip_[font|opacity|fg_color|bg_color|border_width|border_color] -- mouse_finder_[color|timeout|animate_timeout|radius|factor] -- Example: --theme.taglist_bg_focus = "#ff0000" theme.taglist_bg_normal = "#000000" theme.taglist_bg_focus = "#000000" theme.taglist_fg_normal = "#000000" theme.taglist_fg_focus = "#55B043" -- Display the taglist squares theme.taglist_squares_sel = "/usr/share/awesome/themes/default/tagli +st/squarefw.png" theme.taglist_squares_unsel = "/usr/share/awesome/themes/default/tagli +st/squarew.png" theme.tasklist_floating_icon = "/usr/share/awesome/themes/default/task +list/floatingw.png" -- Variables set for theming the menu: -- menu_[bg|fg]_[normal|focus] -- menu_[border_color|border_width] theme.menu_bg_normal = "#000000" theme.menu_bg_focus = "#55B043" theme.menu_fg_normal = "#55B043" theme.menu_fg_focus = "#000000" theme.menu_border_color = "#000000" theme.menu_border_width = "5" -- theme.menu_submenu_icon = "/usr/share/awesome/themes/default/submen +u.png" theme.menu_height = "15" -- default menu_width = 100 theme.menu_width = "150"

Thank you, and again, I apologize if this response is in bad taste.