[gitg/wip/gaurav/clickable-links-subject-textview: 9/15] parse custom links on subject



commit 2fe54a17f0c2562635ec5fcc1d854066f366d7fc
Author: Alberto Fanjul <albertofanjul gmail com>
Date:   Sat Jan 19 15:34:35 2019 +0100

    parse custom links on subject

 libgitg/gitg-diff-view-commit-details.vala | 65 ++++++++++++++++++++++++++++++
 libgitg/gitg-diff-view.vala                |  2 +-
 2 files changed, 66 insertions(+), 1 deletion(-)
---
diff --git a/libgitg/gitg-diff-view-commit-details.vala b/libgitg/gitg-diff-view-commit-details.vala
index 842bf780..26f4d464 100644
--- a/libgitg/gitg-diff-view-commit-details.vala
+++ b/libgitg/gitg-diff-view-commit-details.vala
@@ -272,6 +272,8 @@ class Gitg.DiffViewCommitDetails : Gtk.Grid
                                result = result.replace(text, "<a href=\"%s\">%s</a>".printf(text, text));
                                matchInfo.next();
                        }
+
+                       result = parse_ini_file(result, ".git/config");
                }
                catch(Error e)
                {
@@ -279,6 +281,69 @@ class Gitg.DiffViewCommitDetails : Gtk.Grid
                return result;
        }
 
+       //TODO: pass repository
+       private string parse_ini_file(string subject_text, string config_file)
+       {
+               string result = subject_text.dup();
+               GLib.KeyFile file = new GLib.KeyFile();
+
+               try
+               {
+                       if (file.load_from_file(config_file , GLib.KeyFileFlags.NONE))
+                       {
+                               foreach (string group in file.get_groups())
+                               {
+                                       if (group.has_prefix("gitg.custom-link"))
+                                       {
+                                               string custom_link_regexp = file.get_string (group, "regexp");
+                                               string custom_link_replacement = file.get_string (group, 
"replacement");
+                                               message("found group: %s", custom_link_regexp);
+                                               bool custom_color = file.has_key (group, "color");
+                                               string color = null;
+                                               if (custom_color)
+                                               {
+                                                       string custom_link_color = file.get_string (group, 
"color");
+                                                       color = custom_link_color;
+                                               }
+                                               //TODO: Extract to replace function
+                                               var regex = new Regex (custom_link_regexp);
+                                               try
+                                               {
+                                                       GLib.MatchInfo matchInfo;
+
+                                                       regex.match (subject_text, 0, out matchInfo);
+
+                                                       while (matchInfo.matches ())
+                                                       {
+                                                               string text = matchInfo.fetch(0);
+                                                               string link = text.dup();
+                                                               message("found: %s", link);
+                                                               if (custom_link_replacement != null)
+                                                               {
+                                                                       link = regex.replace(link, 
text.length, 0, custom_link_replacement);
+                                                               }
+                                                               //if (color != null) {
+                                                               //      result = result.replace(text, "<a 
href=\"%s\" title=\"%s\" style=\"color:%s\">%s</a>".printf(link, link, color, text));
+                                                               //} else {
+                                                                       result = result.replace(text, "<a 
href=\"%s\" title=\"%s\">%s</a>".printf(link, link, text));
+                                                               //}
+
+                                                               matchInfo.next();
+                                                       }
+                                               }
+                                               catch(Error e)
+                                               {
+                                               }
+                                       }
+                               }
+                       }
+               } catch (Error e)
+               {
+                       message("Cannot read %s %s", config_file, e.message);
+               }
+               return result;
+       }
+
        private void update_avatar()
        {
                if (commit == null)
diff --git a/libgitg/gitg-diff-view.vala b/libgitg/gitg-diff-view.vala
index e3f70227..cb53862f 100644
--- a/libgitg/gitg-diff-view.vala
+++ b/libgitg/gitg-diff-view.vala
@@ -601,7 +601,7 @@ public class Gitg.DiffView : Gtk.Grid
                        }
                } catch (Error e)
                {
-                       message("Cannot read custom.ini %s", e.message);
+                       message("Cannot read %s: %s", config_file, e.message);
                }
        }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]