Example #1
0
        private static EMElement CreateTOCInline(
            Match match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data)
        {
            var start = 0;
            var end   = 0;

            SpacePattern.Split(match.Groups["params"].Value).ToList().ForEach(
                param =>
            {
                if (string.IsNullOrWhiteSpace(param))
                {
                    return;
                }

                var parts = param.Split(':');
                var name  = parts[0];
                var value = int.Parse(parts[1]);

                switch (name)
                {
                case "start":
                    start = value;
                    break;

                case "end":
                    end = value;
                    break;
                }
            });

            var path = match.Groups["path"].Value;
            ClosestFileStatus status;
            var linkedDoc = data.ProcessedDocumentCache.GetClosest(path, out status);

            if (status == ClosestFileStatus.FileMissing)
            {
                return(EMErrorElement.Create(doc, origin, parent, data, "BadIncludeOrMissingMarkdownFile", path));
            }

            if (status == ClosestFileStatus.ChangedToIntVersion || status == ClosestFileStatus.ExactMatch)
            {
                EMElement output = new EMTOCInline(doc, origin, parent, start, end, linkedDoc, path);

                if (status == ClosestFileStatus.ChangedToIntVersion)
                {
                    output.AddMessage(new EMReadingMessage(MessageClass.Info, "BadIncludeOrMissingMarkdownFileINTUsed", path), data);
                }

                return(output);
            }

            // should never happen!
            throw new InvalidOperationException("Bad linking file status.");
        }
Example #2
0
        private static EMElement CreateFromReference(Match match, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data)
        {
            var alt = match.Groups[2].Value;


            var linkId = match.Groups[3].Value.ToLowerInvariant();

            // for shortcut links like ![this][].
            if (linkId == "")
            {
                linkId = alt.ToLowerInvariant();
            }

            alt = alt.Replace("\"", """);

            if (!data.ReferenceLinks.Urls.ContainsKey(linkId))
            {
                return(EMErrorElement.Create(doc, origin, parent, data, "MissingImageRef", linkId));
            }

            var url = data.ReferenceLinks.Urls[linkId];

            var imageFormat = data.ReferenceLinks.ReferenceAttributes[linkId];

            var width          = imageFormat.Width;
            var height         = imageFormat.Height;
            var convert        = imageFormat.DoCompressImages;
            var convertType    = imageFormat.ConvertImageFormat;
            var convertQuality = imageFormat.ImageQuality;
            var hexFillColor   = imageFormat.ImageFill;

            var options = data.ReferenceLinks.ImageAlignment.ContainsKey(linkId)
                              ? data.ReferenceLinks.ImageAlignment[linkId]
                              : new ImageOptions();

            var title = data.ReferenceLinks.Titles.ContainsKey(linkId)
                            ? Markdown.EscapeBoldItalic(data.ReferenceLinks.Titles[linkId])
                            : null;

            return(Create(
                       data, doc, origin, parent, url, alt, title, convert, width,
                       height, convertQuality, hexFillColor, convertType, options));
        }
Example #3
0
        public static EMElement CreateIncludeFilesFromText(string text, EMDocument doc, EMElementOrigin origin, EMElement parent, TransformationData data)
        {
            var specs = Preprocessor.UnescapeChars(IncludeFilesPattern.Match(text).Groups["specs"].Value, true);

            var paths = GetIncludesFromPathSpecs(specs, data);

            var inclusions = new EMElements(doc, origin, parent);

            var i = 0;

            foreach (var path in paths)
            {
                var pathOrigin = new EMElementOrigin(i, "");

                try
                {
                    bool languageChanged;

                    var excerpt =
                        ExcerptsManager.Get(data.ProcessedDocumentCache,
                                            Path.Combine(data.CurrentFolderDetails.AbsoluteMarkdownPath, path),
                                            data.CurrentFolderDetails.Language,
                                            "",
                                            out languageChanged);

                    inclusions.Add(new EMInclude(doc, pathOrigin, inclusions, excerpt, 0, languageChanged, false));
                }
                catch (ExcerptsManagerException e)
                {
                    inclusions.Add(EMErrorElement.Create(doc, pathOrigin, inclusions, data, e.MessageId, e.MessageArgs));
                }

                ++i;
            }

            return(inclusions);
        }
Example #4
0
        private static EMElement Create(
            Match match,
            EMDocument doc,
            EMElementOrigin origin,
            EMElement parent,
            TransformationData data)
        {
            var includeFileFolderName = "";
            var includeRegion         = "";

            var thisOffset = 0;

            if (match.Groups["includeFileRegion"].Value.Contains('#'))
            {
                includeFileFolderName = match.Groups["includeFileRegion"].Value.Split('#')[0];
                includeRegion         = match.Groups["includeFileRegion"].Value.Split('#')[1];

                if (!includeRegion.StartsWith("doxygen"))
                {
                    includeRegion = includeRegion.ToLower();
                }
            }
            else
            {
                includeFileFolderName = match.Groups["includeFileRegion"].Value;
            }

            if (includeFileFolderName.ToUpper().Contains("%ROOT%"))
            {
                includeFileFolderName = ".";
            }

            if (String.IsNullOrWhiteSpace(includeFileFolderName))
            {
                if (String.IsNullOrWhiteSpace(includeRegion))
                {
                    // Error cannot have no region and no file specified.
                    return(EMErrorElement.Create(
                               data.Document,
                               origin,
                               parent,
                               data,
                               "ExcerptRegionToIncludeWhenNoFileGiven"));
                }

                // Assume that this is a reference to a location in this file.
                includeFileFolderName = doc.GetAbsoluteMarkdownPath();
            }

            if (!String.IsNullOrWhiteSpace(match.Groups["offsetValue"].Value) &&
                !int.TryParse(match.Groups["offsetValue"].Value, out thisOffset))
            {
                return(EMErrorElement.Create(
                           data.Document,
                           origin,
                           parent,
                           data,
                           "ValueMustBeNumber"));
            }

            try
            {
                bool languageChanged;

                var excerpt =
                    ExcerptsManager.Get(data.ProcessedDocumentCache,
                                        Path.Combine(data.CurrentFolderDetails.AbsoluteMarkdownPath, includeFileFolderName),
                                        data.CurrentFolderDetails.Language,
                                        includeRegion,
                                        out languageChanged);

                return(new EMInclude(doc, origin, parent, excerpt, thisOffset, languageChanged));
            }
            catch (ExcerptsManagerException e)
            {
                return(EMErrorElement.Create(doc, origin, parent, data, e.MessageId, e.MessageArgs));
            }
        }
Example #5
0
        private static EMElement Create(
            TransformationData data,
            EMDocument doc,
            EMElementOrigin origin,
            EMElement parent,
            string url,
            string alt,
            string title,
            string convert,
            string width,
            string height,
            string convertQuality,
            string hexFillColor,
            string convertType,
            ImageOptions options)
        {
            ImageFormat imageFormatType;

            if (String.IsNullOrWhiteSpace(url))
            {
                return(EMErrorElement.Create(doc, origin, parent, data, "EmptyLink", origin.Text));
            }

            alt = alt.Replace("\"", """);

            if (title != null)
            {
                title = title.Replace("\"", """);
            }

            if (url.StartsWith("<") && url.EndsWith(">"))
            {
                url = url.Substring(1, url.Length - 2); // Remove <>'s surrounding URL, if present
            }

            if (String.IsNullOrWhiteSpace(alt))
            {
                //if no alt text provided use the file name.
                alt = Regex.Replace(url, @".*[\\|/](.*)", "$1");
            }

            var doConvert = !convert.ToLower().Equals("false") && data.Markdown.DefaultImageDoCompress;

            int widthIntConverted;
            int heightIntConverted;
            int quality;

            Color fillColor;

            if (doConvert)
            {
                //if we are converting try to get the other values
                //try parse the strings for width and height into integers
                try
                {
                    widthIntConverted = Int32.Parse(width);
                }
                catch (FormatException)
                {
                    widthIntConverted = 0;
                }

                try
                {
                    heightIntConverted = Int32.Parse(height);
                }
                catch (FormatException)
                {
                    heightIntConverted = 0;
                }

                try
                {
                    quality = Int32.Parse(convertQuality);
                }
                catch (FormatException)
                {
                    quality = data.Markdown.DefaultImageQuality;
                }

                try
                {
                    fillColor = ImageConversion.GetColorFromHexString(hexFillColor);
                }
                catch (Exception)
                {
                    fillColor = data.Markdown.DefaultImageFillColor;
                }

                if (String.IsNullOrWhiteSpace(convertType))
                {
                    convertType     = data.Markdown.DefaultImageFormatExtension;
                    imageFormatType = data.Markdown.DefaultImageFormat;
                }
                else
                {
                    try
                    {
                        imageFormatType = ImageConversion.GetImageFormat(convertType.ToLower());
                    }
                    catch (Exception)
                    {
                        return(EMErrorElement.Create(
                                   doc,
                                   origin,
                                   parent,
                                   data,
                                   "UnsupportedImageFileTypeConversion",
                                   Markdown.Unescape(url),
                                   convertType.ToLower()));
                    }
                }
            }
            else
            {
                //set width and height to zero indicating to converter that we want to use the images values
                widthIntConverted  = 0;
                heightIntConverted = 0;

                //set conversion type to itself, but do check it is a supported image type.
                convertType =
                    Regex.Match(Markdown.Unescape(url), @"\.(png|gif|tga|bmp|jpg)", RegexOptions.IgnoreCase).Groups[1].Value;

                try
                {
                    imageFormatType = ImageConversion.GetImageFormat(convertType.ToLower());
                }
                catch (Exception)
                {
                    return(EMErrorElement.Create(
                               doc,
                               origin,
                               parent,
                               data,
                               "UnsupportedImageFileTypeConversion",
                               Markdown.Unescape(url),
                               convertType.ToLower()));
                }

                quality   = 100;
                fillColor = data.Markdown.DefaultImageFillColor;
            }

            if (!String.IsNullOrWhiteSpace(convertType))
            {
                try
                {
                    var path = new EMLocalFilePath(Markdown.Unescape(url), doc, data,
                                                   fileName => System.IO.Path.GetFileNameWithoutExtension(fileName) + "." + ImageConversion.GetImageExt(imageFormatType));

                    if (!path.IsImage)
                    {
                        throw new EMPathVerificationException(Language.Message("GivenPathIsNotAnImage", url));
                    }

                    data.ImageDetails.Add(
                        new ImageConversion(
                            path.GetSource(),
                            path.GetAbsolutePath(data),
                            widthIntConverted,
                            heightIntConverted,
                            doConvert,
                            imageFormatType,
                            fillColor,
                            quality));

                    return(new EMImage(doc, origin, parent, path, title, alt, options));
                }
                catch (EMPathVerificationException e)
                {
                    return(new EMErrorElement(doc, origin, parent, e.AddToErrorList(data, origin.Text)));
                }
            }

            throw new InvalidOperationException("Should not happen!");
        }