2009-11-03 08:48:49 +00:00
|
|
|
using System;
|
2014-03-28 19:08:38 +00:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
2009-03-08 00:46:58 +00:00
|
|
|
using System.IO;
|
2014-03-28 19:08:38 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Text;
|
2009-08-17 12:28:22 +00:00
|
|
|
using System.Text.RegularExpressions;
|
2009-03-08 00:46:58 +00:00
|
|
|
using System.Xml;
|
2014-03-28 19:08:38 +00:00
|
|
|
using System.Xml.Linq;
|
|
|
|
using System.Xml.XPath;
|
2009-03-08 00:46:58 +00:00
|
|
|
|
2014-03-28 19:08:38 +00:00
|
|
|
using Bind.Structures;
|
|
|
|
|
2009-03-08 00:46:58 +00:00
|
|
|
namespace Bind
|
|
|
|
{
|
|
|
|
class DocProcessor
|
|
|
|
{
|
2014-03-31 15:09:30 +00:00
|
|
|
static readonly char[] numbers = "0123456789".ToCharArray();
|
2010-11-21 14:24:05 +00:00
|
|
|
static readonly Regex remove_mathml = new Regex(
|
|
|
|
@"<(mml:math|inlineequation)[^>]*?>(?:.|\n)*?</\s*\1\s*>",
|
2009-03-08 00:46:58 +00:00
|
|
|
RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
|
2014-03-30 08:29:11 +00:00
|
|
|
static readonly Regex remove_doctype = new Regex(
|
|
|
|
@"<!DOCTYPE[^>\[]*(\[.*\])?>", RegexOptions.Compiled | RegexOptions.Multiline);
|
2014-03-30 09:01:03 +00:00
|
|
|
static readonly Regex remove_xmlns = new Regex(
|
|
|
|
"xmlns=\".+\"", RegexOptions.Compiled);
|
2009-03-08 00:46:58 +00:00
|
|
|
|
2014-03-31 15:09:30 +00:00
|
|
|
readonly Dictionary<string, string> DocumentationFiles =
|
|
|
|
new Dictionary<string, string>();
|
|
|
|
readonly Dictionary<string, Documentation> DocumentationCache =
|
|
|
|
new Dictionary<string, Documentation>();
|
|
|
|
|
2014-03-28 19:08:38 +00:00
|
|
|
Documentation Cached;
|
2010-10-12 11:00:46 +00:00
|
|
|
string LastFile;
|
|
|
|
|
2014-03-31 15:09:30 +00:00
|
|
|
IBind Generator { get; set; }
|
|
|
|
Settings Settings { get { return Generator.Settings; } }
|
|
|
|
|
|
|
|
public DocProcessor(IBind generator)
|
|
|
|
{
|
|
|
|
if (generator == null)
|
|
|
|
throw new ArgumentNullException();
|
|
|
|
|
|
|
|
Generator = generator;
|
2014-03-31 15:40:15 +00:00
|
|
|
foreach (string file in Directory.GetFiles(Settings.DocPath).Concat(
|
|
|
|
Directory.GetFiles(Settings.FallbackDocPath)))
|
2014-03-31 15:09:30 +00:00
|
|
|
{
|
2014-03-31 15:40:15 +00:00
|
|
|
var name = Path.GetFileName(file);
|
|
|
|
if (!DocumentationFiles.ContainsKey(name))
|
|
|
|
{
|
|
|
|
DocumentationFiles.Add(name, file);
|
|
|
|
}
|
2014-03-31 15:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Documentation Process(Function f, EnumProcessor processor)
|
|
|
|
{
|
|
|
|
Documentation docs = null;
|
|
|
|
|
|
|
|
if (DocumentationCache.ContainsKey(f.WrappedDelegate.Name))
|
|
|
|
{
|
|
|
|
return DocumentationCache[f.WrappedDelegate.Name];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var file = Settings.FunctionPrefix + f.WrappedDelegate.Name + ".xml";
|
|
|
|
if (!DocumentationFiles.ContainsKey(file))
|
|
|
|
file = Settings.FunctionPrefix + f.TrimmedName + ".xml";
|
|
|
|
if (!DocumentationFiles.ContainsKey(file))
|
|
|
|
file = Settings.FunctionPrefix + f.TrimmedName.TrimEnd(numbers) + ".xml";
|
|
|
|
|
|
|
|
docs =
|
|
|
|
(DocumentationFiles.ContainsKey(file) ? ProcessFile(DocumentationFiles[file], processor) : null) ??
|
|
|
|
new Documentation
|
|
|
|
{
|
|
|
|
Summary = String.Empty,
|
|
|
|
Parameters = f.Parameters.Select(p =>
|
|
|
|
new DocumentationParameter(p.Name, String.Empty)).ToList()
|
|
|
|
};
|
|
|
|
|
|
|
|
DocumentationCache.Add(f.WrappedDelegate.Name, docs);
|
|
|
|
}
|
|
|
|
|
|
|
|
return docs;
|
|
|
|
}
|
|
|
|
|
2009-03-08 18:08:35 +00:00
|
|
|
// Strips MathML tags from the source and replaces the equations with the content
|
|
|
|
// found in the <!-- eqn: :--> comments in the docs.
|
|
|
|
// Todo: Some simple MathML tags do not include comments, find a solution.
|
|
|
|
// Todo: Some files include more than 1 function - find a way to map these extra functions.
|
2014-03-31 15:09:30 +00:00
|
|
|
Documentation ProcessFile(string file, EnumProcessor processor)
|
2009-03-08 00:46:58 +00:00
|
|
|
{
|
2013-11-10 08:12:42 +00:00
|
|
|
string text;
|
|
|
|
|
2010-10-12 11:00:46 +00:00
|
|
|
if (LastFile == file)
|
2014-03-28 19:08:38 +00:00
|
|
|
return Cached;
|
2010-10-12 11:00:46 +00:00
|
|
|
|
|
|
|
LastFile = file;
|
2013-11-10 08:12:42 +00:00
|
|
|
text = File.ReadAllText(file);
|
2009-03-08 00:46:58 +00:00
|
|
|
|
2014-03-28 19:08:38 +00:00
|
|
|
text = text
|
2014-03-30 09:01:03 +00:00
|
|
|
.Replace("ε", "epsilon") // Fix unrecognized ε entities
|
|
|
|
.Replace("xml:", String.Empty); // Remove namespaces
|
2014-03-30 08:29:11 +00:00
|
|
|
text = remove_doctype.Replace(text, String.Empty);
|
2014-03-30 09:01:03 +00:00
|
|
|
text = remove_xmlns.Replace(text, string.Empty);
|
2014-03-28 19:08:38 +00:00
|
|
|
|
2013-11-10 08:12:42 +00:00
|
|
|
Match m = remove_mathml.Match(text);
|
2009-03-08 00:46:58 +00:00
|
|
|
while (m.Length > 0)
|
|
|
|
{
|
2013-11-10 08:12:42 +00:00
|
|
|
string removed = text.Substring(m.Index, m.Length);
|
|
|
|
text = text.Remove(m.Index, m.Length);
|
2009-03-08 18:08:35 +00:00
|
|
|
int equation = removed.IndexOf("eqn");
|
|
|
|
if (equation > 0)
|
|
|
|
{
|
2010-11-21 14:24:05 +00:00
|
|
|
// Find the start and end of the equation string
|
|
|
|
int eqn_start = equation + 4;
|
|
|
|
int eqn_end = removed.IndexOf(":-->") - equation - 4;
|
|
|
|
if (eqn_end < 0)
|
|
|
|
{
|
|
|
|
// Note: a few docs from man4 delimit eqn end with ": -->"
|
|
|
|
eqn_end = removed.IndexOf(": -->") - equation - 4;
|
|
|
|
}
|
|
|
|
if (eqn_end < 0)
|
|
|
|
{
|
|
|
|
Console.WriteLine("[Warning] Failed to find equation for mml.");
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
|
|
|
string eqn_substring = removed.Substring(eqn_start, eqn_end);
|
2013-11-10 08:12:42 +00:00
|
|
|
text = text.Insert(m.Index, "<![CDATA[" + eqn_substring + "]]>");
|
2009-03-08 18:08:35 +00:00
|
|
|
}
|
2010-11-21 14:24:05 +00:00
|
|
|
|
|
|
|
next:
|
2013-11-10 08:12:42 +00:00
|
|
|
m = remove_mathml.Match(text);
|
2009-03-08 00:46:58 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 19:08:38 +00:00
|
|
|
XDocument doc = null;
|
2009-03-08 18:08:35 +00:00
|
|
|
try
|
|
|
|
{
|
2014-03-28 19:08:38 +00:00
|
|
|
doc = XDocument.Parse(text);
|
2014-03-31 15:09:30 +00:00
|
|
|
Cached = ToInlineDocs(doc, processor);
|
2014-03-28 19:08:38 +00:00
|
|
|
return Cached;
|
2009-03-08 18:08:35 +00:00
|
|
|
}
|
2014-03-30 08:29:11 +00:00
|
|
|
catch (Exception e)
|
2009-03-08 00:46:58 +00:00
|
|
|
{
|
2009-03-08 18:08:35 +00:00
|
|
|
Console.WriteLine(e.ToString());
|
|
|
|
Console.WriteLine(doc.ToString());
|
2014-03-28 19:08:38 +00:00
|
|
|
return null;
|
2009-03-08 00:46:58 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-28 19:08:38 +00:00
|
|
|
|
2014-03-31 15:09:30 +00:00
|
|
|
Documentation ToInlineDocs(XDocument doc, EnumProcessor enum_processor)
|
2014-03-28 19:08:38 +00:00
|
|
|
{
|
2014-03-31 15:09:30 +00:00
|
|
|
if (doc == null || enum_processor == null)
|
|
|
|
throw new ArgumentNullException();
|
|
|
|
|
|
|
|
var no_const_processing = Settings.Legacy.NoAdvancedEnumProcessing | Settings.Legacy.ConstIntEnums;
|
|
|
|
if (!Generator.Settings.IsEnabled(no_const_processing))
|
|
|
|
{
|
|
|
|
// Translate all GL_FOO_BAR constants according to EnumProcessor
|
|
|
|
foreach (var e in doc.XPathSelectElements("//constant"))
|
|
|
|
{
|
|
|
|
var c = e.Value;
|
|
|
|
if (c.StartsWith(Settings.ConstantPrefix))
|
|
|
|
{
|
|
|
|
// Remove "GL_" from the beginning of the string
|
|
|
|
c = c.Replace(Settings.ConstantPrefix, String.Empty);
|
|
|
|
}
|
|
|
|
e.Value = enum_processor.TranslateConstantName(c, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create inline documentation
|
2014-03-28 19:08:38 +00:00
|
|
|
var inline = new Documentation
|
|
|
|
{
|
|
|
|
Summary =
|
2014-03-30 08:29:11 +00:00
|
|
|
Cleanup(
|
2014-03-30 09:01:03 +00:00
|
|
|
((IEnumerable)doc.XPathEvaluate("/refentry/refnamediv/refpurpose"))
|
2014-03-30 08:29:11 +00:00
|
|
|
.Cast<XElement>().First().Value),
|
2014-03-28 19:08:38 +00:00
|
|
|
Parameters =
|
2014-03-30 17:16:30 +00:00
|
|
|
((IEnumerable)doc.XPathEvaluate("/refentry/refsect1[@id='parameters']/variablelist/varlistentry/term/parameter"))
|
|
|
|
.Cast<XElement>()
|
|
|
|
.Select(p =>
|
|
|
|
new DocumentationParameter(
|
|
|
|
p.Value.Trim(),
|
|
|
|
Cleanup(p.XPathSelectElement("../../listitem").Value)))
|
2014-03-28 19:08:38 +00:00
|
|
|
.ToList()
|
|
|
|
};
|
|
|
|
|
|
|
|
inline.Summary = Char.ToUpper(inline.Summary[0]) + inline.Summary.Substring(1);
|
|
|
|
return inline;
|
|
|
|
}
|
2014-03-30 08:29:11 +00:00
|
|
|
|
|
|
|
static readonly char[] newline = new char[] { '\n' };
|
|
|
|
static string Cleanup(string text)
|
|
|
|
{
|
|
|
|
return
|
|
|
|
String.Join(" ", text
|
|
|
|
.Replace("\r", "\n")
|
|
|
|
.Split(newline, StringSplitOptions.RemoveEmptyEntries)
|
2014-03-31 07:51:15 +00:00
|
|
|
.Select(s => s.Trim()).ToArray())
|
|
|
|
.Trim();
|
2014-03-30 08:29:11 +00:00
|
|
|
}
|
2009-03-08 00:46:58 +00:00
|
|
|
}
|
|
|
|
}
|