Пример #1
0
        private List <string> Parse(IEnumerable <string> fileLines)
        {
            List <StylesheetEntry> entries = SplitStylesheet(fileLines);

            HashSet <string> foundStyles = new HashSet <string>();
            List <string>    errors      = new List <string>();
            bool             foundMarker = false;

            for (int i = 0; i < entries.Count; ++i)
            {
                StylesheetEntry entry = entries[i];

                if (entry.Marker != "marker")
                {
                    continue;
                }
                string[] parts = entry.Text.Split();
                if (parts.Length > 1 && parts[1] == "-")
                {
                    // If the entry looks like "\marker xy -" remove the tag and its end tag if any
                    foundMarker = true;
                    RemoveTag(parts[0]);
                    RemoveTag(parts[0] + "*");
                    continue;
                }

                foundMarker = true;
                ScrTag        tag = CreateTag(entry.Text);
                List <string> tagErrors;
                ScrTag        endTag = ScrTag.ParseSTYMarkerEntry(tag, entries, i + 1, out tagErrors);

                errors.AddRange(tagErrors);

                if (endTag != null && !tagIndexDictionary.ContainsKey(endTag.Marker))
                {
                    tags.Add(endTag);
                    tagIndexDictionary[endTag.Marker] = tags.Count - 1;
                }

                if (foundStyles.Contains(entry.Text))
                {
                    errors.Add(ScrTag.GetMessage(entry.LineNumber,
                                                 string.Format(Localizer.Str("Duplicate style definition '{0}'"), entry.Text)));
                }

                foundStyles.Add(entry.Text);
            }

            if (!foundMarker)
            {
                errors.Add(ScrTag.GetMessage(1, Localizer.Str("No styles defined")));
                return(errors);
            }

            return(errors.Count == 0 ? null : errors);
        }
Пример #2
0
        private static int ParseI(StylesheetEntry entry)
        {
            int result;

            if (!Int32.TryParse(entry.Text, NumberStyles.Integer, CultureInfo.InvariantCulture, out result) || result < 0)
            {
                throw new ArgumentOutOfRangeException("entry", entry.Text, "");
            }

            return(result);
        }
Пример #3
0
        private static int ParseF(StylesheetEntry entry)
        {
            float result;

            if (!Single.TryParse(entry.Text, NumberStyles.Float | NumberStyles.AllowThousands, CultureInfo.InvariantCulture, out result))
            {
                throw new ArgumentOutOfRangeException("entry", entry.Text, "");
            }

            return((int)(result * 1000));
        }
Пример #4
0
        private static void ParseTextType(ScrTag qTag, StylesheetEntry entry)
        {
            string text = entry.Text.ToLowerInvariant();

            if (text == "chapternumber")
            {
                qTag.AddTextProperty(TextProperties.scChapter);
            }
            if (text == "versenumber")
            {
                qTag.AddTextProperty(TextProperties.scVerse);
            }

            qTag.TextType = ParseEnum(entry.Text, propToTextType);
        }
Пример #5
0
        private static int ParseColor(StylesheetEntry entry)
        {
            int result;

            if (entry.Text.Length <= 1 || entry.Text[0] != 'x')
            {
                result = ParseI(entry);
                // Integer codes have been in B G R order, so convert this to the standard order
                int b = (result >> 16) & 0xFF;
                int g = (result >> 8) & 0xFF;
                int r = result & 0xFF;
                result = (r << 16) | (g << 8) | b;
            }
            else if (!Int32.TryParse(entry.Text.Substring(1), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out result) || result < 0)
            {
                throw new ArgumentOutOfRangeException("entry", entry.Text, "");
            }

            return(result);
        }
Пример #6
0
        private static void ParseTextProperties(ScrTag qTag, StylesheetEntry entry)
        {
            string text = entry.Text.ToLowerInvariant();

            string[] parts = text.Split();

            foreach (string part in parts)
            {
                if (part.Trim() == "")
                {
                    continue;
                }

                qTag.AddTextProperty(ParseEnum(part, propToTextProps));
            }

            if (qTag.HasTextProperty(TextProperties.scNonpublishable))
            {
                qTag.RemoveTextProperty(TextProperties.scPublishable);
            }
        }
Пример #7
0
        /// <summary>
        /// ScrTag as parsed from the specified stylesheet entries.
        /// </summary>
        /// <param name="qTag">qTag needs to have the Marker set when calling this method</param>
        /// <param name="stylesheetEntries"></param>
        /// <param name="entryIndex"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        internal static ScrTag ParseSTYMarkerEntry(ScrTag qTag, List <StylesheetEntry> stylesheetEntries, int entryIndex,
                                                   out List <string> errors)
        {
            int markerLineNumber = stylesheetEntries[entryIndex - 1].LineNumber;

            // The following items are present for conformance with
            // Paratext release 5.0 stylesheets.  Release 6.0 and later
            // follows the guidelines set in InitPropertyMaps.
            // Make sure \id gets book property
            if (qTag.Marker == "id")
            {
                qTag.AddTextProperty(TextProperties.scBook);
            }

            errors = new List <string>();
            HashSet <string> foundAttribs  = new HashSet <string>();
            ScrTag           qTagEndMarker = null;

            while (entryIndex < stylesheetEntries.Count)
            {
                StylesheetEntry entry = stylesheetEntries[entryIndex];
                ++entryIndex;

                if (entry.Marker == "marker")
                {
                    break;
                }

                if (foundAttribs.Contains(entry.Marker))
                {
                    errors.Add(GetMessage(entry.LineNumber, string.Format(Localizer.Str("Duplicate style attribute '{0}'"), entry.Marker)));
                }

                try
                {
                    switch (entry.Marker)
                    {
                    case "name": qTag.Name = entry.Text; break;

                    case "description": qTag.Description = entry.Text; break;

                    case "fontname": qTag.Fontname = entry.Text; break;

                    case "fontsize": qTag.FontSize = entry.Text == "-" ? 0 : ParseI(entry); break;

                    case "xmltag": qTag.XMLTag = entry.Text; break;

                    case "encoding": qTag.Encoding = entry.Text; break;

                    case "linespacing": qTag.LineSpacing = ParseI(entry); break;

                    case "spacebefore": qTag.SpaceBefore = ParseI(entry); break;

                    case "spaceafter": qTag.SpaceAfter = ParseI(entry); break;

                    case "leftmargin": qTag.LeftMargin = ParseF(entry); break;

                    case "rightmargin": qTag.RightMargin = ParseF(entry); break;

                    case "firstlineindent": qTag.FirstLineIndent = ParseF(entry); break;

                    case "rank": qTag.Rank = entry.Text == "-" ? 0 : ParseI(entry); break;

                    case "bold": qTag.Bold = (entry.Text != "-"); break;

                    case "smallcaps": qTag.SmallCaps = (entry.Text != "-"); break;

                    case "subscript": qTag.Subscript = (entry.Text != "-"); break;

                    case "italic": qTag.Italic = (entry.Text != "-"); break;

                    // FB 23177 - added the \Regular tag so that there is a way to reset Italic, Bold and Superscript
                    // that is compatible with the ptx2pdf macros used by PrintDraft
                    case "regular":
                        qTag.Italic  = qTag.Bold = qTag.Superscript = false;
                        qTag.Regular = true;
                        break;

                    case "underline": qTag.Underline = (entry.Text != "-"); break;

                    case "superscript": qTag.Superscript = (entry.Text != "-"); break;

                    case "testylename": break;     // Ignore this tag, later we will use it to tie to FW styles

                    case "notrepeatable": qTag.NotRepeatable = (entry.Text != "-"); break;

                    case "textproperties": ParseTextProperties(qTag, entry); break;

                    case "texttype": ParseTextType(qTag, entry); break;

                    case "color": qTag.RawColor = entry.Text == "-" ? 0 : ParseColor(entry); break;

                    case "colorname": qTag.RawColor = entry.Text == "-" ? 0 : GetThemeColor(entry); break;

                    case "justification": qTag.JustificationType = ParseEnum(entry.Text, propToJustification); break;

                    case "styletype": qTag.StyleType = ParseEnum(entry.Text, propToStyleType); break;

                    case "attributes":
                        try
                        {
                            qTag.RawAttributes = entry.Text;
                        }
                        catch (ArgumentException e)
                        {
                            errors.Add(GetMessage(entry.LineNumber, e.Message));
                        }
                        break;

                    case "occursunder":
                        qTag.OccursUnder = String.Join(" ", entry.Text.Split(spaceSep, StringSplitOptions.RemoveEmptyEntries));
                        break;

                    case "endmarker":
                        qTagEndMarker  = MakeEndMarker(entry.Text);
                        qTag.Endmarker = entry.Text;
                        break;

                    default:
                        errors.Add(GetMessage(entry.LineNumber, string.Format(Localizer.Str("Unknown marker: {0}"), entry.Marker)));
                        break;
                    }
                }
                catch (ArgumentOutOfRangeException e)
                {
                    errors.Add(GetMessage(entry.LineNumber,
                                          string.Format(Localizer.Str("Invalid definition for marker '{0}': {1}"), entry.Marker, e.ActualValue)));
                }
                foundAttribs.Add(entry.Marker);
            }

            if (string.IsNullOrEmpty(qTag.Name))
            {
                errors.Add(GetMessage(markerLineNumber, string.Format(Localizer.Str("Missing name for style: {0}"), qTag.Marker)));
            }

            // If we have not seen an end marker but this is a character style
            if (qTag.StyleType == ScrStyleType.scCharacterStyle && qTagEndMarker == null)
            {
                string endMarker = qTag.Marker + "*";
                qTagEndMarker  = MakeEndMarker(endMarker);
                qTag.Endmarker = endMarker;
            }
            else if (qTag.StyleType == ScrStyleType.scMilestone)
            {
                if (qTagEndMarker != null)
                {
                    qTagEndMarker.StyleType     = ScrStyleType.scMilestoneEnd;
                    qTagEndMarker.RawAttributes = "?id"; // id is always an optional attribute for the end marker
                    qTagEndMarker.Name          = qTag.Name;
                }
                else
                {
                    errors.Add(GetMessage(markerLineNumber,
                                          string.Format(Localizer.Str("Missing end marker for style: {0}"), qTag.Marker)));
                }
            }

            // Special cases
            if (qTag.TextType == ScrTextType.scOther &&
                !qTag.HasTextProperty(TextProperties.scNonpublishable) &&
                !qTag.HasTextProperty(TextProperties.scChapter) &&
                !qTag.HasTextProperty(TextProperties.scVerse) &&
                (qTag.StyleType == ScrStyleType.scCharacterStyle || qTag.StyleType == ScrStyleType.scParagraphStyle))
            {
                qTag.AddTextProperty(TextProperties.scPublishable);
            }

            return(qTagEndMarker);
        }
Пример #8
0
 private static int GetThemeColor(StylesheetEntry entry)
 {
     return(0);
 }