예제 #1
0
        /// <summary>
        /// Searches Attributes value for requested named attribute
        /// </summary>
        /// <param name="attributes">Attributes to be searched</param>
        /// <param name="name">name of attribute</param>
        /// <returns>the requested value or an empty string if not found</returns>
        public static string GetAttribute(NamedAttribute[] attributes, Enum <AttributeName> name)
        {
            if (attributes == null || attributes.Length == 0)
            {
                return("");
            }

            NamedAttribute attribute = attributes.FirstOrDefault(a => a.Name == name);

            return(attribute?.Value ?? "");
        }
예제 #2
0
        public bool SetAttributes(string attributesValue, Enum <AttributeName> defaultAttributeName, ref string adjustedText, bool preserveWhitespace = false)
        {
            if (string.IsNullOrEmpty(attributesValue))
            {
                return(false);
            }


            // for figures, convert 2.0 format to 3.0 format. Will need to write this as the 2.0 format
            // if the project is not upgrated.
            if (NestlessMarker == "fig" && attributesValue.Count(c => c == '|') == 5)
            {
                List <NamedAttribute> attributeList = new List <NamedAttribute>(6);
                string[] parts = attributesValue.Split('|');
                AppendAttribute(attributeList, "alt", adjustedText);
                AppendAttribute(attributeList, "src", parts[0]);
                AppendAttribute(attributeList, "size", parts[1]);
                AppendAttribute(attributeList, "loc", parts[2]);
                AppendAttribute(attributeList, "copy", parts[3]);
                string whitespace = "";
                if (preserveWhitespace)
                {
                    whitespace = adjustedText.Substring(0, adjustedText.Length - adjustedText.TrimStart().Length);
                }
                adjustedText = whitespace + parts[4];
                AppendAttribute(attributeList, "ref", parts[5]);
                Attributes = attributeList.ToArray();
                return(true);
            }

            Match match = attributeRegex.Match(attributesValue);

            if (!match.Success || match.Length != attributesValue.Length)
            {
                return(false); // must match entire string
            }
            Group defaultValue = match.Groups["default"];

            if (defaultValue.Success)
            {
                // only accept default value it there is a defined default attribute
                if (defaultAttributeName != Enum <AttributeName> .Null)
                {
                    Attributes = new[] { new NamedAttribute(defaultAttributeName, defaultValue.Value) };
                    this.defaultAttributeName = defaultAttributeName;
                    return(true);
                }
                return(false);
            }

            CaptureCollection attributeNames  = match.Groups["name"].Captures;
            CaptureCollection attributeValues = match.Groups["value"].Captures;

            if (attributeNames.Count == 0 || attributeNames.Count != attributeValues.Count)
            {
                return(false);
            }

            this.defaultAttributeName = defaultAttributeName;
            NamedAttribute[] attributes = new NamedAttribute[attributeNames.Count];
            for (int i = 0; i < attributeNames.Count; i++)
            {
                attributes[i] = new NamedAttribute(attributeNames[i].Value, attributeValues[i].Value, attributeValues[i].Index);
            }
            Attributes = attributes;
            return(true);
        }
예제 #3
0
        public int GetAttributeOffset(Enum <AttributeName> attributeName)
        {
            NamedAttribute attribute = Attributes?.FirstOrDefault(a => a.Name == attributeName);

            return(attribute?.Offset ?? 0);
        }