1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
//
// A provider that uses Windows help file xhtml TOC files and looks for the
// referenced documents to create the help source.
//
// Authors:
// Copyright 2003 Lee Mallabone <gnome@fonicmonkey.net>
// Johannes Roith <johannes@roith.de>
// Miguel de Icaza <miguel@ximian.com>
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
namespace Monodoc.Providers
{
public class XhtmlProvider : Provider
{
string tocFile;
readonly XNamespace ns = "http://www.w3.org/1999/xhtml";
public XhtmlProvider (string handbookTocFile)
{
tocFile = handbookTocFile;
if (!File.Exists (tocFile))
throw new FileNotFoundException (String.Format ("The table of contents, `{0}' does not exist", tocFile));
}
public override void PopulateTree (Tree tree)
{
var doc = XDocument.Load (tocFile);
var uls = doc.Descendants (ns + "body").First ().Elements (ns + "ul");
foreach (var ul in uls)
ParseUl (tree, tree.RootNode, ul);
}
void ParseUl (Tree tree, Node parent, XElement ul)
{
var storage = tree.HelpSource.Storage;
foreach (var e in ul.Elements (ns + "li")) {
var inner = e.Element (ns + "object");
if (inner == null)
continue;
string caption, element;
ObjectEntryToParams (inner, out caption, out element);
// Don't add if the backing file doesn't exist
if (!File.Exists (element)) {
Console.Error.WriteLine ("Warning: File `{0}' referenced in TOC but it doesn't exist. It will be ignored.", element);
continue;
}
using (var file = File.OpenRead (element))
storage.Store (element, file);
parent.CreateNode (caption, XhtmlHelpSource.XhtmlPrefix + element);
}
}
void ObjectEntryToParams (XElement obj, out string caption, out string element)
{
var ps = obj.Elements (ns + "param");
caption = ps
.Where (p => p.Attribute ("name").Value == "Name")
.Select (p => (string)p.Attribute ("value"))
.FirstOrDefault ();
caption = caption ?? string.Empty;
element = ps
.Where (p => p.Attribute ("name").Value == "Local")
.Select (p => (string)p.Attribute ("value"))
.FirstOrDefault ();
element = element ?? string.Empty;
}
public override void CloseTree (HelpSource hs, Tree tree)
{
}
}
public class XhtmlHelpSource : HelpSource
{
public XhtmlHelpSource (string base_file, bool create) : base (base_file, create)
{
}
internal const string XhtmlPrefix = "xhtml:";
protected override string UriPrefix {
get {
return XhtmlPrefix;
}
}
public override SortType SortType {
get {
return SortType.Element;
}
}
public override DocumentType GetDocumentTypeForId (string id)
{
return id == "root:" ? DocumentType.TocXml : DocumentType.MonoBook;
}
public override bool IsGeneratedContent (string id)
{
return id == "root:";
}
public override string GetText (string url)
{
return TreeDumper.ExportToTocXml (Tree.RootNode, "Mono Handbook", string.Empty);
}
public static string GetAbsoluteLink(string target, string url)
{
string value = null;
if (target.StartsWith ("#") ||
target.StartsWith ("T:") ||
target.StartsWith ("M:") ||
target.StartsWith ("P:") ||
target.StartsWith ("T:") ||
target.StartsWith ("E:") ||
target.StartsWith ("F:") ||
target.StartsWith ("O:") ||
target.StartsWith ("N:") ||
target.StartsWith ("api:"))
return null;
int endp = target.IndexOf(':');
if (endp == -1)
endp = 0;
string protocol = target.Substring(0, endp);
switch (protocol) {
case "mailto":
case "http":
case "https":
case "ftp":
case "news":
case "irc":
break;
default:
// handle absolute urls like: /html/en/images/empty.png
if (!target.StartsWith("/")) {
// url is something like "gnome/bindings/mono.html"
// This will get the path "gnome/bindings"
int slash = url.LastIndexOf ("/");
string tmpurl = url;
if (slash != -1)
tmpurl = url.Substring(0, slash);
// Count "../" in target and go one level down
// for each in tmpurl, eventually, then remove "../".
Regex reg1 = new Regex("../");
MatchCollection matches = reg1.Matches(target);
for(int i = 1; i < matches.Count; i++) {
slash = tmpurl.LastIndexOf ("/");
if (slash != -1)
tmpurl = tmpurl.Substring(0, slash);
}
target = target.Replace("../", "");
value = tmpurl + "/" + target;
} else {
value = target.Substring(1, target.Length - 1);
}
break;
}
return value;
}
XmlDocument RewriteLinks(XmlDocument docToProcess, string url)
{
XmlNodeList nodeList = docToProcess.GetElementsByTagName("a");
foreach(XmlNode node in nodeList) {
XmlElement element = (XmlElement) node;
if (element.HasAttribute("href") ){
XmlAttribute href = element.GetAttributeNode("href");
string target = href.Value;
target = GetAbsoluteLink(target, url);
if (target != null) {
string newtarget = String.Format ("source-id:{0}:xhtml:{1}", SourceID, target);
href.Value = newtarget;
}
}
}
nodeList = docToProcess.GetElementsByTagName("img");
foreach(XmlNode node in nodeList) {
XmlElement element = (XmlElement) node;
if (element.HasAttribute("src") ){
XmlAttribute href = element.GetAttributeNode("src");
string target = href.Value;
target = GetAbsoluteLink(target, url);
if (target != null) {
string newtarget = String.Format ("source-id:{0}:xhtml:{1}", SourceID, target);
href.Value = newtarget;
}
}
}
return docToProcess;
}
public override void PopulateIndex (IndexMaker index_maker)
{
PopulateIndexFromNodes (Tree.RootNode);
}
void PopulateIndexFromNodes (Node start)
{
/*var nodes = start.Nodes;
if (nodes != null) {
foreach (Node n in nodes)
PopulateIndexFromNodes (n);
}*/
}
}
}
|