Treat grammars differently according to what they generate
authorEnrico Zini <enrico@enricozini.org>
Thu, 13 Aug 2009 15:58:51 +0000 (16:58 +0100)
committerEnrico Zini <enrico@enricozini.org>
Thu, 13 Aug 2009 15:58:51 +0000 (16:58 +0100)
src/app_polygen.vala
src/update-polygen-info

index 7ee4ea86cae8691e5a999eaa998995240ef86d31..3b2538f2db32086b9af87d276203e59c471895fc 100644 (file)
@@ -30,12 +30,12 @@ protected class PolygenPage : Object
     public Gtk.ListStore model;
     public Gtk.TreeView list;
     public Gtk.ScrolledWindow scroll;
-    public signal void selected(string page, string name);
+    public signal void selected(string page, string name, string type);
 
     public PolygenPage(string name)
     {
         this.name = name;
-        model = new Gtk.ListStore(2, typeof(string), typeof(string));
+        model = new Gtk.ListStore(3, typeof(string), typeof(string), typeof(string));
         list = new Gtk.TreeView.with_model(model);
         list.insert_column_with_attributes (-1, "Name", new Gtk.CellRendererText(), "text", 0);
         list.insert_column_with_attributes (-1, "Title", new Gtk.CellRendererText(), "text", 1);
@@ -50,9 +50,10 @@ protected class PolygenPage : Object
     {
         Gtk.TreeIter iter;
         if (!model.get_iter(out iter, path)) return;
-        Value grm_name;
+        Value grm_name, grm_type;
         model.get_value(iter, 0, out grm_name);
-        selected(name, (string)grm_name);
+        model.get_value(iter, 2, out grm_type);
+        selected(name, (string)grm_name, (string)grm_type);
     }
 }
 
@@ -61,19 +62,16 @@ protected class PolygenRun : Gtk.VBox
     public Gtk.ScrolledWindow scroll;
     protected Gtk.TextBuffer text_buffer;
     protected Gtk.TextView text;
+    protected Regex unhtml;
 
-    private string _grammar;
-    public string grammar {
-        get { return _grammar; }
-        set {
-            _grammar = value;
-            update();
-        }
-    }
+    public string grm_name { get; set; }
+    public string grm_type { get; set; }
 
     public PolygenRun()
     {
-        _grammar = "";
+        grm_name = "";
+        grm_type = "";
+        unhtml = new Regex("[ ]*<[^>]+>[ ]*", 0, 0);
         text_buffer = new Gtk.TextBuffer(null);
         text = new Gtk.TextView.with_buffer(text_buffer);
         text.wrap_mode = Gtk.WrapMode.WORD;
@@ -95,17 +93,31 @@ protected class PolygenRun : Gtk.VBox
 
     public void update()
     {
-        string[] args = new string[3];
+        if (grm_name == "") return;
+        string[] args = new string[5];
         args[0] = "/usr/bin/polygen";
-        args[1] = "/usr/share/polygen/" + _grammar + ".grm";
-        args[2] = null;
+        args[1] = "/usr/share/polygen/" + grm_name + ".grm";
+        if (grm_type == "line/text" || grm_type == "line/html")
+        {
+            args[2] = "-X";
+            args[3] = "10";
+            args[4] = null;
+        }
+        else
+            args[2] = null;
         int pipe_out;
         Process.spawn_async_with_pipes(config.homedir, args, null, 0, null, null, null, out pipe_out, null);
         IOChannel ch = new IOChannel.unix_new(pipe_out);
         string result;
-        ch.read_to_end(out result, null);
+        size_t res_len;
+        ch.read_to_end(out result, out res_len);
         ch.shutdown(false);
 
+        if (grm_type == "line/html" || grm_type == "block/html")
+        {
+            result = unhtml.replace(result, (long)res_len, 0, " ", 0);
+        }
+
         text_buffer.text = result;
         Gtk.TextIter iter;
         text_buffer.get_iter_at_offset(out iter, 0);
@@ -120,7 +132,7 @@ public class Polygen : Applet
     protected Gtk.Notebook notebook;
     protected PolygenRun result;
 
-    private void add(string page, string name, string title)
+    private void add_grammar(string page, string name, string type, string title)
     {
         PolygenPage pg = null;
         foreach (PolygenPage p in pages)
@@ -142,11 +154,14 @@ public class Polygen : Applet
         pg.model.append (out iter);
         pg.model.set(iter, 0, name);
         pg.model.set(iter, 1, title);
+        pg.model.set(iter, 2, type);
     }
 
-    protected void on_selected(string page, string name)
+    protected void on_selected(string page, string name, string type)
     {
-        result.grammar = page + "/" + name;
+        result.grm_name = page + "/" + name;
+        result.grm_type = type;
+        result.update();
         notebook.set_current_page(notebook.get_n_pages()-1);
     }
 
@@ -160,11 +175,11 @@ public class Polygen : Applet
             string line;
             var res = data.read_line(out line, null, null);
             if (res != IOStatus.NORMAL) break;
-            string[] vals = line.split(" ", 2);
+            string[] vals = line.split(" ", 3);
             if (vals == null) break;
             string[] np = vals[0].split("/", 2);
             if (np == null) break;
-            add(np[0], np[1], vals[1].strip());
+            add_grammar(np[0], np[1], vals[1], vals[2].strip());
         }
 
         notebook = new Gtk.Notebook();
index eaaf94f63b5079b0fac71ad9e2328c8ec6b262c9..6b91f5d6ecb05c5a173213e9cdf128d8f3ef130b 100755 (executable)
@@ -10,5 +10,19 @@ do
        TITLE=`polygen -info $NAME | grep ^title: | head -1 | sed -re 's/^[^:]+: *//'`
        NAME=${NAME:2}
        NAME=${NAME%.grm}
-       echo "$NAME $TITLE"
+
+       if grep -Fqx $NAME ~/.zavai/polygen-line-text
+       then
+               TYPE="line/text"
+       elif grep -Fqx $NAME ~/.zavai/polygen-line-html
+       then
+               TYPE="line/html"
+       elif grep -Fqx $NAME ~/.zavai/polygen-block-html
+       then
+               TYPE="block/html"
+       else
+               TYPE="block/text"
+       fi
+
+       echo "$NAME $TYPE $TITLE"
 done