C# to update TOC and document IDs in T3D-Documentation

3rd party tools like asset generators, editors.
1 post Page 1 of 1
rlranft
Posts: 298
Joined: Thu Feb 05, 2015 3:11 pm
 
by rlranft » Sun Jun 26, 2016 1:31 am
I use this to keep the side-bar in the docs in sync with the documents - so that the right bit rolls out and stuff is all lined up right:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace tocUpdate
{
    class Program
    {
        static void Main(string[] args)
        {
            if(args.Length < 1)
                return;

            String path = args[0];

            List<String> lines = new List<String>();
            StreamReader sr = new StreamReader(path + "\\toc.html");
            while (!sr.EndOfStream)
            {
                lines.Add(sr.ReadLine());
            }
            sr.Close();

            int docLine = 0;
            for(int i = 0; i < lines.Count; ++i)
            {
                if(lines.Contains("id = \"doc"))
                {
                    // this has a doc id
                    int start = 0;
                    int end = 0;
                    int count = 0;
                    int index = 0;
                    while(count <= 3)
                    {
                        if(lines[index] == '\"')
                        {
                            count++;
                        }
                        else if(count == 3)
                        {
                            start = index;
                            count++;
                            break;
                        }
                        index++;
                    }
                    while(count <= 4)
                    {
                        if (lines[index] == '\"')
                        {
                            end = index;
                            count++;
                            break;
                        }
                        index++;
                    }
                    String sub = lines.Substring(start, (end - start));
                    lines = lines.Replace(sub, "doc" + docLine);

                    // now find the referenced page and update the document id in the script section
                    int hrefStart = lines.IndexOf("href =");
                    int hrefEnd = hrefStart + 1;
                    char quote = lines[hrefStart];
                    while (quote != '\"')
                    {
                        ++hrefStart;
                        quote = lines[hrefStart];
                        hrefEnd = hrefStart + 1;
                    }
                    // should now have index of starting quote for doc path
                    quote = lines[hrefEnd];
                    while (quote != '\"')
                    {
                        ++hrefEnd;
                        quote = lines[i][hrefEnd];
                    }
                    // and the end of the doc path
                    List<String> docLines = new List<String>();
                    String targetDoc = lines[i].Substring(hrefStart + 1, (hrefEnd - hrefStart) - 1);
                    targetDoc = targetDoc.Replace('/', '\\');
                    using (StreamReader docReader = new StreamReader(path + "\\" + targetDoc))
                    {
                        bool inScript = false;
                        while(!docReader.EndOfStream)
                        {
                            String line = docReader.ReadLine();
                            if (line.Contains("<script>"))
                                inScript = true;
                            if (line.Contains("</script>"))
                                inScript = false;
                            if (!inScript)
                                docLines.Add(line);
                            else
                            {
                                // look for our doc id and update to new id
                                if(line.Contains("pageID"))
                                {
                                    String substr = line.Substring(line.IndexOf("pageID"));
                                    line = line.Replace(substr, "pageID = " + docLine + ";");
                                }
                                if(line.Contains("'doc"))
                                {
                                    String substr = line.Substring(line.IndexOf("'doc"));
                                    line = line.Replace(substr, "'doc" + docLine + "');");
                                }
                                docLines.Add(line);
                            }
                        }
                    }
                    using (StreamWriter docWriter = new StreamWriter(path + "\\" + targetDoc))
                    {
                        foreach(String line in docLines)
                        {
                            docWriter.WriteLine(line);
                        }
                    }
                    ++docLine;

                }

                StreamWriter sw = new StreamWriter(path + "\\toc.html");
                foreach (String line in lines)
                    sw.WriteLine(line);
                sw.Flush();
                sw.Close();
            }
        }
    }
}

In case anyone is planning to add stuff to https://github.com/dottools/Torque3D-Documentation.
1 post Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests