Example #1
0
        public void Compile(string[] lines, Preprocessor preprocessor)
        {
            _settingAttributesParser.Parse(lines);

            HelpsTitle = _settingAttributesParser.GetSingleValue(AttributesTitle);

            var output_values = _settingAttributesParser.GetValues(AttributesOutput);

            ChmOutput  = output_values.Contains(OutputValueChm);
            HtmlOutput = output_values.Contains(OutputValueHtml);
            PdfOutput  = output_values.Contains(OutputValuePdf);

            DefaultTopic  = (ChmOutput || HtmlOutput) ? preprocessor.GetTopic(GetRequiredTopic(AttributesDefaultTopic)) : null;
            PdfCoverTopic = PdfOutput ? preprocessor.GetTopic(GetRequiredTopic(AttributesPdfCoverTopic)) : null;

            _definitions = new Dictionary <string, string>();

            // add the definitions in the settings file
            foreach (var kp in _settingAttributesParser.GetPairs())
            {
                if (_settingsAttributes.FirstOrDefault(x => (kp.Key == x.Name)) == null)
                {
                    _definitions.Add(kp.Key, kp.Value[0]);
                }
            }

            foreach (var definitionFilename in _settingAttributesParser.GetValues(AttributesDefinitionsFile))
            {
                _definitionAttributesParser.Parse(File.ReadAllLines(definitionFilename));

                foreach (var kp in _definitionAttributesParser.GetPairs())
                {
                    if (_definitions.ContainsKey(kp.Key))
                    {
                        throw new Exception($"The definition {kp.Key} cannot be specified in multiple files");
                    }

                    _definitions.Add(kp.Key, kp.Value[0]);
                }
            }

            // add the resource header IDs
            _resourceIdSet = new ResourceIdSet();

            foreach (var resourceFileProjectName in _settingAttributesParser.GetValues(AttributesResourceFile))
            {
                _resourceIdSet.AddProject(resourceFileProjectName);
            }
        }
Example #2
0
        private void webBrowser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            // open topics in the topic editor
            if (e.Url.OriginalString.IndexOf(TopicUrlPrefix, StringComparison.InvariantCultureIgnoreCase) == 0)
            {
                e.Cancel = true;

                try
                {
                    string filename = e.Url.OriginalString.Substring(TopicUrlPrefix.Length);
                    ((MainForm)this.ParentForm).ShowOrCreateForm(_preprocessor.GetTopic(filename));
                }

                catch (Exception)
                {
                }
            }
        }
Example #3
0
        public void Compile(string[] lines, Preprocessor preprocessor)
        {
            _settingAttributesParser.Parse(lines);

            HelpsTitle = _settingAttributesParser.GetSingleValue(AttributesTitle);

            var output_values = _settingAttributesParser.GetValues(AttributesOutput);

            ChmOutput  = output_values.Contains(OutputValueChm);
            HtmlOutput = output_values.Contains(OutputValueHtml);
            PdfOutput  = output_values.Contains(OutputValuePdf);

            DefaultTopic  = (ChmOutput || HtmlOutput) ? preprocessor.GetTopic(GetRequiredTopic(AttributesDefaultTopic)) : null;
            PdfCoverTopic = PdfOutput ? preprocessor.GetTopic(GetRequiredTopic(AttributesPdfCoverTopic)) : null;

            _definitions = new Dictionary <string, string>();

            // add the definitions in the settings file
            foreach (var kp in _settingAttributesParser.GetPairs())
            {
                if (_settingsAttributes.FirstOrDefault(x => (kp.Key == x.Name)) == null)
                {
                    _definitions.Add(kp.Key, kp.Value[0]);
                }
            }

            foreach (var definitionFilename in _settingAttributesParser.GetValues(AttributesDefinitionsFile))
            {
                _definitionAttributesParser.Parse(File.ReadAllLines(definitionFilename));

                foreach (var kp in _definitionAttributesParser.GetPairs())
                {
                    if (_definitions.ContainsKey(kp.Key))
                    {
                        throw new Exception($"The definition {kp.Key} cannot be specified in multiple files");
                    }

                    _definitions.Add(kp.Key, kp.Value[0]);
                }
            }

            _resourceFiles   = new List <string>();
            _resourceFileIds = new HashSet <string>();

            foreach (string resourceFilename in _settingAttributesParser.GetValues(AttributesResourceFile))
            {
                string evaluatedResourceFilename = EvaluateResourceFilename(resourceFilename);

                if (File.Exists(evaluatedResourceFilename))
                {
                    _resourceFiles.Add(evaluatedResourceFilename);
                    ProcessResourceFile(evaluatedResourceFilename);
                }

                else
                {
                    throw new Exception(String.Format("The resource file could not be located: {0}{1}", evaluatedResourceFilename,
                                                      (_resourceFileRootDirectory == null) ? ("\r\n\r\nPerhaps add a resource file root directory specification file with the name " + Colorizer.Constants.ResourceFileRootDirectoryFilename) : ""));
                }
            }
        }
Example #4
0
        public TopicListNode Parse(string[] lines, Preprocessor preprocessor)
        {
            string fileDescription = _indexParser ? "index" : "table of contents";

            TopicListNode        rootNode    = null;
            List <TopicListNode> parentNodes = new List <TopicListNode>();

            for (int i = 0; i < lines.Length; i++)
            {
                string line = lines[i];

                int zeroBasedLevel = 0; // the level is zero-based (but displayed as one-based)

                for ( ; zeroBasedLevel < line.Length && line[zeroBasedLevel] == '\t'; zeroBasedLevel++)
                {
                    ;
                }

                line = line.Trim();

                int hashPos = line.IndexOf('#');

                if (hashPos >= 0)  // a comment
                {
                    line = line.Substring(0, hashPos).Trim();
                }

                if (line.Length == 0)  // an empty line
                {
                    continue;
                }

                try
                {
                    if (parentNodes.Count == 0 && zeroBasedLevel >= 1)
                    {
                        throw new Exception("must be at level 1 because it is the first node");
                    }

                    if (_indexParser && zeroBasedLevel >= 2)
                    {
                        throw new Exception(String.Format("has more than the maximum allowed levels ({0} > 2)", zeroBasedLevel + 1));
                    }

                    if (zeroBasedLevel > parentNodes.Count)
                    {
                        throw new Exception(String.Format("cannot increase more than one level from {0} to {1}", parentNodes.Count, zeroBasedLevel + 1));
                    }

                    int pipePos = line.IndexOf('|');

                    string filename = (pipePos >= 0) ? line.Substring(0, pipePos).Trim() : line;
                    string title    = (pipePos >= 0) ? line.Substring(pipePos + 1).Trim() : null;

                    if (title != null && String.IsNullOrWhiteSpace(title))
                    {
                        throw new Exception("has a blank title");
                    }

                    Preprocessor.TopicPreprocessor topic = String.IsNullOrWhiteSpace(filename) ? null : preprocessor.GetTopic(filename);
                    bool isLinkToChm = (title != null && GetLinkToChm(title) != null);

                    if (isLinkToChm && zeroBasedLevel > 0)
                    {
                        throw new Exception("is invalid because links to other help files must only be at level one");
                    }

                    if (_indexParser)  // index
                    {
                        // an index entry must be either a topic or a link to a help file
                        if (topic == null && !isLinkToChm)
                        {
                            throw new Exception("must specify a topic filename");
                        }
                    }

                    TopicListNode newNode = new TopicListNode(topic, title);

                    if (rootNode == null)  // the root node
                    {
                        rootNode = newNode;
                        parentNodes.Add(rootNode);
                    }

                    else if (zeroBasedLevel == parentNodes.Count)  // child node
                    {
                        if (!_indexParser && parentNodes[zeroBasedLevel - 1].Topic != null)
                        {
                            throw new Exception("is invalid because nothing can come at a level under a topic");
                        }

                        parentNodes[zeroBasedLevel - 1].ChildNode = newNode;
                        parentNodes.Add(newNode);
                    }

                    else // sibling node (at the same or previous level)
                    {
                        while (parentNodes.Count > (zeroBasedLevel + 1))
                        {
                            parentNodes.RemoveAt(parentNodes.Count - 1);
                        }

                        if (!_indexParser && newNode.Topic != null && parentNodes[zeroBasedLevel].Topic == null)
                        {
                            throw new Exception("is invalid because a topic cannot come after a chapter at the same level");
                        }

                        parentNodes[zeroBasedLevel].SiblingNode = newNode;
                        parentNodes[zeroBasedLevel]             = newNode;
                    }
                }

                catch (Exception exception)
                {
                    throw new Exception(String.Format("The {0} line #{1} \"{2}\" {3}", fileDescription, i + 1, line, exception.Message));
                }
            }

            if (rootNode == null)
            {
                throw new Exception(String.Format("The {0} does not have any entries", fileDescription));
            }

            // make sure that every topic is included
            HashSet <Preprocessor.TopicPreprocessor> allTopics = preprocessor.GetAllTopics(false);

            RemoveUsedTopics(rootNode, allTopics);

            StringBuilder sb = new StringBuilder();

            foreach (Preprocessor.TopicPreprocessor topic in allTopics)
            {
                if (!topic.Optional)
                {
                    sb.AppendLine(Path.GetFileName(topic.Filename));
                }
            }

            if (sb.Length > 0)
            {
                throw new Exception(String.Format("The {0} is missing entries for the topics:\r\n\r\n{1}", fileDescription, sb.ToString()));
            }

            return(rootNode);
        }