diff options
author | Mario Sopena Novales <masono@mono-cvs.ximian.com> | 2005-09-05 18:28:29 +0400 |
---|---|---|
committer | Mario Sopena Novales <masono@mono-cvs.ximian.com> | 2005-09-05 18:28:29 +0400 |
commit | 6fea1de94d2910cdbfad0b8f99d8796402150851 (patch) | |
tree | 59350b3e482a12be2af3aa0f96d94f7363465ec6 /docbrowser | |
parent | d8d3d0d2be7bf135237c52f2ed1efc70cea48df2 (diff) |
2005-09-05 Mario Sopena Novales <mario.sopena@gmail.com>
Implement basic searching capabilities
* browser.cs:
- Added a new "--make-search-index" parameter
- Added a TreeView to show the search results
- Added a function to Highlight the search result
* browser.glade:
- Repair index and search icons
- Added a TreeView to show the search results
* monodoc.in:
- Added a new "--make-search-index" parameter
svn path=/trunk/mono-tools/; revision=49473
Diffstat (limited to 'docbrowser')
-rw-r--r-- | docbrowser/ChangeLog | 12 | ||||
-rw-r--r-- | docbrowser/browser.cs | 144 | ||||
-rw-r--r-- | docbrowser/browser.glade | 125 | ||||
-rw-r--r-- | docbrowser/monodoc.in | 9 |
4 files changed, 274 insertions, 16 deletions
diff --git a/docbrowser/ChangeLog b/docbrowser/ChangeLog index 035fa8ba..52e014e1 100644 --- a/docbrowser/ChangeLog +++ b/docbrowser/ChangeLog @@ -1,3 +1,15 @@ +2005-09-05 Mario Sopena Novales <mario.sopena@gmail.com> + Implement basic searching capabilities + * browser.cs: + - Added a new "--make-search-index" parameter + - Added a TreeView to show the search results + - Added a function to Highlight the search result + * browser.glade: + - Repair index and search icons + - Added a TreeView to show the search results + * monodoc.in: + - Added a new "--make-search-index" parameter + 2005-08-22 Mario Sopena Novales <mario.sopena@gmail.com> * browser.cs: - Update the treeview everytime we change the tab diff --git a/docbrowser/browser.cs b/docbrowser/browser.cs index bbf42c75..24382566 100644 --- a/docbrowser/browser.cs +++ b/docbrowser/browser.cs @@ -47,6 +47,10 @@ class Driver { RootTree.MakeIndex (); return 0; + case "--make-search-index": + RootTree.MakeSearchIndex (); + return 0; + case "--help": Console.WriteLine ("Options are:\n"+ "browser [--html TOPIC] [--make-index] [TOPIC] [--merge-changes CHANGE_FILE TARGET_DIR+]"); @@ -148,6 +152,16 @@ class Browser { Gdk.Pixbuf monodoc_pixbuf; + // + // Used for searching + // + [Glade.Widget] Entry search_term; + [Glade.Widget] TreeView search_tree; + [Glade.Widget] ScrolledWindow scrolledwindow_search; + TreeStore search_store; + SearchableIndex search_index; + string highlight_text; + // // Left-hand side Browsers // @@ -261,6 +275,21 @@ class Browser { // // Other bits // + search_index = help_tree.GetSearchIndex(); + if (search_index == null) { + search_term.Editable = false; + Gtk.Label l = new Gtk.Label ("<b>No search index found</b>\n\n" + + "as root, run:\n\n monodoc --make-search-index\n\nto create the index"); + l.UseMarkup = true; + l.Show (); + scrolledwindow_search.Remove (search_tree); + scrolledwindow_search.Add (l); + } else { + search_store = new TreeStore (typeof (string)); + search_tree.Model = search_store; + search_tree.AppendColumn ("Searches", new CellRendererText(), "text", 0); + search_tree.Selection.Changed += new EventHandler (ShowSearchResult); + } bookList = new ArrayList (); index_browser = IndexBrowser.MakeIndexBrowser (this); @@ -311,6 +340,62 @@ class Browser { if (tree_browser.SelectedNode != CurrentTab.CurrentNode) tree_browser.ShowNode (CurrentTab.CurrentNode); } + + // + // Invoked when the user presses enter on the search_entry + // + void OnSearchActivated (object sender, EventArgs a) + { + search_tree.Model = null; + search_term.Editable = false; + string term = search_term.Text; + //search in the index + Result r = search_index.Search (term); + if (r == null) + return; //There was a problem with the index + //insert the results in the tree + TreeIter iter; + + int max = r.Count > 500? 500:r.Count; + iter = search_store.AppendValues (r.Term + " (" + max + " hits)"); + for (int i = 0; i < max; i++) + search_store.AppendValues (iter, r.GetTitle(i)); + + // Show the results + search_tree.Model = search_store; + search_tree.CollapseAll(); + TreePath p = search_store.GetPath (iter); + search_tree.ExpandToPath (p); + search_tree.Selection.SelectPath (p); + search_term.Editable = true; + } + // + // Invoked when the user click on one of the search results + // + void ShowSearchResult (object sender, EventArgs a) + { + CurrentTab.SetMode (Mode.Viewer); + + Gtk.TreeIter iter; + Gtk.TreeModel model; + + bool selected = search_tree.Selection.GetSelected (out model, out iter); + if (!selected) + return; + + TreePath p = model.GetPath (iter); + if (p.Depth < 2) + return; + int i_0 = p.Indices [0]; + int i_1 = p.Indices [1]; + Result res = (Result) search_index.Results [i_0]; + TreeIter parent; + model.IterParent (out parent, iter); + string term = (string) search_store.GetValue (parent, 0); + highlight_text = term.Substring (0, term.IndexOf ("(")-1); + LoadUrl (res.GetUrl (i_1)); + } + // // Reload current page // @@ -455,6 +540,9 @@ class Browser { { CurrentUrl = url; CurrentTab.CurrentNode = matched_node; + if (highlight_text != null) + text = DoHighlightText (text); + CurrentTab.html.Render(text); if (matched_node != null) { if (tree_browser.SelectedNode != matched_node) @@ -502,6 +590,62 @@ class Browser { } // + // Highlights the text of the search + // + // we have to highligh everything that is not inside < and > + string DoHighlightText (string text) { + System.Text.StringBuilder sb = new System.Text.StringBuilder (text); + + //search for the term to highlight in a lower case version of the text + string text_low = text.ToLower(); + string term_low = highlight_text.ToLower(); + + //search for < and > so we dont substitute text of html tags + ArrayList lt = new ArrayList(); + ArrayList gt = new ArrayList(); + int ini = 0; + ini = text_low.IndexOf ('<', ini, text_low.Length); + while (ini != -1) { + lt.Add (ini); + ini = text_low.IndexOf ('<', ini+1, text_low.Length-ini-1); + } + ini = 0; + ini = text_low.IndexOf ('>', ini, text_low.Length); + while (ini != -1) { + gt.Add (ini); + ini = text_low.IndexOf ('>', ini+1, text_low.Length-ini-1); + } + //start searching for the term + int offset = 0; + int p = 0; + ini = 0; + ini = text_low.IndexOf (term_low, ini, text_low.Length); + while (ini != -1) { + bool beforeLt = ini < (int) lt [p]; + //look if term is inside any html tag + while (!beforeLt) { + bool afterGt = ini > (int) gt [p]; + if (afterGt) { + p++; + beforeLt = ini < (int) lt [p]; + continue; + } else { + goto ExtLoop; + } + } + string t = sb.ToString (ini + offset, term_low.Length); + sb.Remove (ini + offset, term_low.Length); + sb.Insert (ini + offset, "<span style=\"background: yellow\">" + t + "</span>"); + offset += 40; //due to the <span> tag inserted + +ExtLoop: + ini = text_low.IndexOf (term_low, ini+1, text_low.Length-ini-1); + } + + highlight_text = null; //only highlight when a search result is clicked + return sb.ToString(); + } + // // Invoked when the mouse is over a link // string last_url = ""; diff --git a/docbrowser/browser.glade b/docbrowser/browser.glade index 2d5323d1..cbf1100e 100644 --- a/docbrowser/browser.glade +++ b/docbrowser/browser.glade @@ -607,7 +607,7 @@ <child> <widget class="GtkImage" id="image47"> <property name="visible">True</property> - <property name="stock">gtk-find</property> + <property name="stock">gtk-index</property> <property name="icon_size">1</property> <property name="xalign">0.5</property> <property name="yalign">0.5</property> @@ -799,18 +799,119 @@ </child> <child> - <widget class="GtkLabel" id="label5"> + <widget class="GtkVBox" id="search_vbox"> + <property name="border_width">3</property> <property name="visible">True</property> - <property name="label" translatable="yes">n/a</property> - <property name="use_underline">False</property> - <property name="use_markup">False</property> - <property name="justify">GTK_JUSTIFY_LEFT</property> - <property name="wrap">False</property> - <property name="selectable">False</property> - <property name="xalign">0.5</property> - <property name="yalign">0.5</property> - <property name="xpad">0</property> - <property name="ypad">0</property> + <property name="homogeneous">False</property> + <property name="spacing">0</property> + + <child> + <widget class="GtkHBox" id="hbox35"> + <property name="border_width">3</property> + <property name="visible">True</property> + <property name="homogeneous">False</property> + <property name="spacing">3</property> + + <child> + <widget class="GtkImage" id="image133"> + <property name="visible">True</property> + <property name="stock">gtk-find</property> + <property name="icon_size">1</property> + <property name="xalign">0.5</property> + <property name="yalign">0.5</property> + <property name="xpad">0</property> + <property name="ypad">0</property> + </widget> + <packing> + <property name="padding">0</property> + <property name="expand">False</property> + <property name="fill">True</property> + </packing> + </child> + + <child> + <widget class="GtkLabel" id="label66"> + <property name="visible">True</property> + <property name="label" translatable="yes">_Search for:</property> + <property name="use_underline">True</property> + <property name="use_markup">False</property> + <property name="justify">GTK_JUSTIFY_LEFT</property> + <property name="wrap">False</property> + <property name="selectable">False</property> + <property name="xalign">0</property> + <property name="yalign">0.5</property> + <property name="xpad">0</property> + <property name="ypad">0</property> + <property name="mnemonic_widget">index_entry</property> + <property name="ellipsize">PANGO_ELLIPSIZE_NONE</property> + <property name="width_chars">-1</property> + <property name="single_line_mode">False</property> + <property name="angle">0</property> + </widget> + <packing> + <property name="padding">0</property> + <property name="expand">True</property> + <property name="fill">True</property> + </packing> + </child> + </widget> + <packing> + <property name="padding">0</property> + <property name="expand">False</property> + <property name="fill">False</property> + </packing> + </child> + + <child> + <widget class="GtkEntry" id="search_term"> + <property name="visible">True</property> + <property name="can_focus">True</property> + <property name="editable">True</property> + <property name="visibility">True</property> + <property name="max_length">0</property> + <property name="text" translatable="yes"></property> + <property name="has_frame">True</property> + <property name="invisible_char">*</property> + <property name="activates_default">False</property> + <signal name="activate" handler="OnSearchActivated" last_modification_time="Wed, 13 Jul 2005 23:36:43 GMT"/> + </widget> + <packing> + <property name="padding">3</property> + <property name="expand">False</property> + <property name="fill">True</property> + </packing> + </child> + + <child> + <widget class="GtkScrolledWindow" id="scrolledwindow_search"> + <property name="visible">True</property> + <property name="can_focus">True</property> + <property name="hscrollbar_policy">GTK_POLICY_ALWAYS</property> + <property name="vscrollbar_policy">GTK_POLICY_ALWAYS</property> + <property name="shadow_type">GTK_SHADOW_IN</property> + <property name="window_placement">GTK_CORNER_TOP_LEFT</property> + <signal name="row_activated" handler="ShowSearchResult" /> + + <child> + <widget class="GtkTreeView" id="search_tree"> + <property name="visible">True</property> + <property name="can_focus">True</property> + <property name="headers_visible">False</property> + <property name="rules_hint">False</property> + <property name="reorderable">False</property> + <property name="enable_search">True</property> + <property name="fixed_height_mode">False</property> + <property name="hover_selection">False</property> + <property name="hover_expand">False</property> + </widget> + </child> + </widget> + <packing> + <property name="padding">0</property> + <property name="expand">True</property> + <property name="fill">True</property> + </packing> + </child> </widget> <packing> <property name="tab_expand">False</property> diff --git a/docbrowser/monodoc.in b/docbrowser/monodoc.in index f9fd63cd..932776e9 100644 --- a/docbrowser/monodoc.in +++ b/docbrowser/monodoc.in @@ -43,10 +43,11 @@ case x$1 in echo " TOPIC Start the browser at TOPIC" echo " (ex. N:System, T:System.Object, M:System.Object.Equals," echo " and P: for properties, F: for fields, E: for events, etc.)" - echo " --help Print this message" - echo " --html TOPIC Print the HTML contents of TOPIC" - echo " --make-index Create the documentation index" - echo " --no-gecko Don't use Mozilla to render the contents" + echo " --help Print this message" + echo " --html TOPIC Print the HTML contents of TOPIC" + echo " --make-index Create the documentation index" + echo " --make-search-index Create the searchable documentation index" + echo " --no-gecko Don't use Mozilla to render the contents" echo echo "The following options are available for authoring documentation:" echo " --edit path Edit (unassembled) documentation at path" |