コード例 #1
0
 public IDocumentExpression CreateExpression(DocumentToken replacementKeyToken, List <DocumentToken> parameters, List <DocumentToken> body)
 {
     return(new FuncDocExpression((context) => { return new ConsoleString(DateTime.Now.Date.ToString()); }));
 }
コード例 #2
0
 public IDocumentExpression CreateExpression(DocumentToken replacementKeyToken, List <DocumentToken> parameters, List <DocumentToken> body)
 {
     return(new FuncDocExpression((context) => { return new ConsoleString("if override"); }));
 }
コード例 #3
0
        /// <summary>
        /// Do the documentation parsing.
        /// </summary>
        /// <param name="sourceFile"></param>
        static void Documentify(string sourceFile)
        {
            int tail = sourceFile.LastIndexOf('.');

            if (tail <= 0)
            {
                throw new Exception("Can't find a file name dot in sourceFile");
            }
            int backslash = sourceFile.LastIndexOf('\\');

            string mdStr = sourceFile.Substring(0, tail) + ".md";

            // Read the file.
            string[] lines = File.ReadAllLines(sourceFile, Encoding.UTF8);

            //string xmlStr = sourceFile.Substring(0, tail) + ".xml";

            bool          inComments = false;
            DocumentToken docToken   = null;

            // Iterate over the lines to assemble the list of potential documentations.
            for (int i = 0; i < lines.Length; ++i)
            {
                string token = lines[i].Trim();
                if (inComments)
                {
                    if (token.StartsWith("///"))
                    {
                        docToken.rawSummary.AppendLine(token.Substring(3));
                    }
                    else
                    {
                        if (token != "[MoonSharpHidden]")
                        {
                            docToken.rawName = token;
                            tokens.Add(docToken);
                        }
                        inComments = false;
                    }
                }
                else
                {
                    if (token.StartsWith("///"))
                    {
                        inComments          = true;
                        docToken            = new DocumentToken();
                        docToken.rawSummary = new StringBuilder();
                        docToken.rawSummary.AppendLine("<token>");
                        docToken.rawSummary.AppendLine(token.Substring(3));
                    }
                }
            }
            docToken = null;

            List <string> contents = new List <string>();

            //System.Console.WriteLine("Potential Tokens:");
            StringBuilder docString = new StringBuilder();

            docString.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            docString.Append("<documentor file=\"");
            string sourceFileName = sourceFile.Substring(backslash + 1);

            docString.Append(sourceFileName);
            docString.AppendLine("\">");
            for (int i = 0; i < tokens.Count; ++i)
            {
                //System.Console.WriteLine(string.Format("[{0,3}]: {1}", i, tokens[i].rawName));
                int openParen = tokens[i].rawName.IndexOf('(');
                if (openParen > 0)
                {
                    int lastSpace = tokens[i].rawName.LastIndexOf(' ', openParen);
                    if (lastSpace >= 0)
                    {
                        tokens[i].rawSummary.Append("<method>");
                        tokens[i].rawSummary.Append(tokens[i].rawName.Substring(lastSpace + 1));
                        tokens[i].rawSummary.AppendLine("</method>");
                    }
                }
                else if (tokens[i].rawName.StartsWith("#region"))
                {
                    tokens[i].rawSummary.Append("<region>");
                    string regionName = tokens[i].rawName.Substring(tokens[i].rawName.IndexOf(' ') + 1);
                    contents.Add(regionName);
                    tokens[i].rawSummary.Append(regionName);
                    tokens[i].rawSummary.AppendLine("</region>");
                }
                tokens[i].rawSummary.AppendLine("</token>");
                docString.Append(tokens[i].rawSummary.ToString());
            }
            docString.AppendLine("</documentor>");
            string rawDocument = docString.ToString();

            // Uncomment to spew the XML document to a file for perusal.
            //File.WriteAllText(xmlStr, rawDocument, Encoding.UTF8);

            XmlDocument doc = new XmlDocument();

            doc.PreserveWhitespace = true;
            try
            {
                doc.LoadXml(rawDocument);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e);
                tokens.Clear();
                return;
            }

            string  luaNamespace = string.Empty;
            XmlNode root         = doc.DocumentElement;

            docString = new StringBuilder();
            docString.Append("# ");
            docString.AppendLine(sourceFileName);
            docString.AppendLine();

            if (contents.Count > 1)
            {
                docString.AppendLine("## Contents");
                docString.AppendLine();
                for (int i = 0; i < contents.Count; ++i)
                {
                    docString.Append("* [");
                    docString.Append(contents[i]);
                    docString.Append("](#");
                    string   anchor      = contents[i].ToLower().Replace(' ', '-');
                    string[] finalAnchor = anchor.Split(',');
                    foreach (string a in finalAnchor)
                    {
                        docString.Append(a);
                    }
                    docString.AppendLine("-category)");
                }
                docString.AppendLine();
            }

            if (root.HasChildNodes)
            {
                XmlNode child = root.FirstChild;
                while (child != null)
                {
                    if (child is XmlElement && child.Name == "token")
                    {
                        IndexToken indexEntry = ParseToken(child, docString, ref luaNamespace);
                        if (indexEntry != null)
                        {
                            indexEntry.sourceFile = sourceFileName;
                            if (indexEntry.indexType == IndexToken.IndexType.INDEX_FUNCTION)
                            {
                                index.Add(indexEntry);
                            }
                            else if (indexEntry.indexType == IndexToken.IndexType.INDEX_REGION)
                            {
                                region.Add(indexEntry);
                            }
                        }
                        //System.Console.WriteLine(child.Name);
                    }
                    child = child.NextSibling;
                }
            }
            docString.AppendLine("***");
            DateTime now = DateTime.UtcNow;

            docString.AppendLine(string.Format("*This documentation was automatically generated from source code at {0,2:00}:{1,2:00} UTC on {2}/{3}/{4}.*",
                                               now.Hour, now.Minute, now.Day, MonthAbbr[now.Month], now.Year));
            docString.AppendLine();
            if (!File.Exists(mdStr) || (File.Exists(mdStr) && File.GetLastWriteTimeUtc(mdStr) <= File.GetLastWriteTimeUtc(sourceFile)))
            {
                File.WriteAllText(mdStr, docString.ToString(), Encoding.UTF8);
                anyWritten = true;
            }

            tokens.Clear();
        }