bool OpenOutFile(string sGenSubDirectory, string fileEnding)
        {
            try
            {
                var filename = _generatorParams.sourceFileTitle_;
                if (!filename.EndsWith(fileEnding))
                {
                    filename = filename + fileEnding;
                }

                var htmlDir = PegUtils.MakeFileName(
                    "", _generatorParams.outputDirectory_, sGenSubDirectory);

                _outputFileName = PegUtils.MakeFileName(
                    filename, htmlDir);

                if (!Directory.Exists(htmlDir))
                {
                    Directory.CreateDirectory(htmlDir);
                }
                _outFile = new StreamWriter(_outputFileName);
            }
            catch (Exception e)
            {
                _outFile.WriteLine("FATAL from <PEG_GENERATOR> FILE:'{0}' could not be opened (%s)", _outputFileName, e.Message);
                return(false);
            }
            return(true);
        }
        private string GetListContinuation(PegNode node)
        {
            var listHead = string.Empty;

            var listbody      = new List <string>();
            var listBlockNode = PegUtils.FindNode(node, (int)EMarkdown.ListBlock);

            //TODO 暂时简化点,只检查第一个元素
            var enumeratorReg = new Regex("[0-9]+.");
            var bulletReg     = new Regex("[+*-]");


            if (listBlockNode.child_.id_ == (int)EMarkdown.Symbol)
            {
                listHead = templates[MarkdownKind.BulletList].sCodeTemplate;
            }
            else
            {
                listHead = templates[MarkdownKind.OrderedList].sCodeTemplate;
            }

            if (listBlockNode != null)
            {
                for (; listBlockNode != null; listBlockNode = PegUtils.FindNodeNext(listBlockNode, (int)EMarkdown.ListBlock))
                {
                    var blockstring = GetListBlock(listBlockNode);

                    var bulletMatch = bulletReg.Match(blockstring);
                    if (bulletMatch != Match.Empty)
                    {
                        if (bulletMatch.Index == 0)
                        {
                            blockstring = blockstring.Remove(bulletMatch.Index, bulletMatch.Length);
                        }
                    }

                    var enumeratorMatch = enumeratorReg.Match(blockstring);
                    if (enumeratorMatch != Match.Empty)
                    {
                        if (enumeratorMatch.Index == 0)
                        {
                            blockstring = blockstring.Remove(enumeratorMatch.Index, enumeratorMatch.Length);
                        }
                    }


                    listbody.Add(
                        templates[MarkdownKind.ListItem].sCodeTemplate
                        .Replace("${ListItemBody}", blockstring));
                }
            }

            return(listHead.Replace("${ListBody}", string.Join("", listbody)));
        }
        private string GetListItem(PegNode node)
        {
            //TODO  inline
            string itemString;

            var listBlock = PegUtils.FindNode(node, (int)EMarkdown.ListBlock);

            itemString = GetListBlock(listBlock);

            var contiNode = PegUtils.FindNode(node, (int)EMarkdown.ListContinuationBlock);

            if (contiNode != null)
            {
                itemString = itemString + "\n" + GetListContinuation(contiNode);
            }

            return(templates[MarkdownKind.ListItem].sCodeTemplate.Replace("${ListItemBody}", itemString));
        }
Exemplo n.º 4
0
        void IParserPostProcessor.Postprocess(ParserPostProcessParams postProcessorParams)
        {
            context_ = new TreeContext(postProcessorParams);
            string outDir = PegUtils.MakeFileName("", context_.generatorParams_.outputDirectory_, "DefiniteLengthForm");

            if (!Directory.Exists(outDir))
            {
                Directory.CreateDirectory(outDir);
            }
            string outFile = PegUtils.MakeFileName(context_.generatorParams_.sourceFileTitle_, outDir);

            using (BinaryWriter rw = new BinaryWriter(File.Open(outFile, FileMode.Create)))
            {
                WriteDefinite(rw, context_.generatorParams_.root_);
                context_.generatorParams_.errOut_.WriteLine("INFO  from <BER_DEFINITE_ENCODER> {0} bytes written to '{1}'",
                                                            rw.BaseStream.Position, outFile);
            }
        }
        private string GetList(PegNode node)
        {
            var content  = string.Empty;
            var listHead = string.Empty;

            if (node.id_ == (int)EMarkdown.OrderedList)
            {
                listHead = templates[MarkdownKind.OrderedList].sCodeTemplate;
            }
            else
            {
                listHead = templates[MarkdownKind.BulletList].sCodeTemplate;
            }


            var listbody = new List <string>();

            var listItemTight = PegUtils.FindNode(node, (int)EMarkdown.ListItemTight);

            if (listItemTight != null)
            {
                for (; listItemTight != null; listItemTight = PegUtils.FindNodeNext(listItemTight, (int)EMarkdown.ListItemTight))
                {
                    listbody.Add(GetListItem(listItemTight));
                }
            }

            var listItem = PegUtils.FindNode(node, (int)EMarkdown.ListItem);

            if (listItem != null)
            {
                for (; listItem != null; listItem = PegUtils.FindNodeNext(listItem, (int)EMarkdown.ListItem))
                {
                    listbody.Add(GetListItem(listItem));
                }
            }


            content = listHead.Replace("${ListBody}", string.Join("", listbody));

            return(content);
        }
        string GenHeading(PegNode heading)
        {
            var content = string.Empty;

            if (heading.child_.id_ == (int)EMarkdown.AtxHeading)
            {
                var headerLevelStr = PegUtils.FindNode(heading.child_, (int)EMarkdown.AtxStart)
                                     .GetAsString(_src);
                var headerLevel = headerLevelStr.Length;
                var headText    = heading.GetAsString(_src).Replace("#", "").Trim();
                content = CreateHeading(headerLevel, headText);
            }
            else
            {
                var selectBottom = PegUtils.FindNode(heading, (int)EMarkdown.SetextBottom1, (int)EMarkdown.SetextBottom2);
                var headText     = PegUtils.GetAsString(_src, heading, selectBottom).Trim();
                content = CreateHeading(2, headText);
            }
            return(content);
        }
Exemplo n.º 7
0
        string GenHeading(PegNode heading)
        {
            var content = string.Empty;
            var level   = _headLevel[(EHtml)heading.child_.id_];

            var header = new string('#', level);

            if (heading.child_.id_ == (int)EHtml.HtmlBlockH1)
            {
                var headerLevelStr = PegUtils.FindNode(heading.child_, (int)EHtml.HtmlBlockH1)
                                     .GetAsString(_src);
                var headerLevel = headerLevelStr.Length;
                var headText    = heading.GetAsString(_src).Replace("#", "").Trim();
                content = CreateHeading(headerLevel, headText);
            }
            else
            {
                var selectBottom = PegUtils.FindNode(heading, (int)EHtml.HtmlBlockH1, (int)EHtml.HtmlBlockH1);
                var headText     = PegUtils.GetAsString(_src, heading, selectBottom).Trim();
                content = CreateHeading(2, headText);
            }
            return(content);
        }