Beispiel #1
0
        /**
         * Inserts a Bibtex string at the given index.
         */
        public void addString(BibtexString str)
        {
            lock (_lock) {
            if (hasStringLabel(str.getName())){
                throw new KeyCollisionException("A string with this label already exists,");
            }

            if (_strings.ContainsKey(str.getId()))
                throw new KeyCollisionException("Duplicate BibtexString id.");

            _strings.Add(str.getId(), str);
            }
        }
Beispiel #2
0
        private static void writeString(TextWriter fw, BibtexString bs, Dictionary<string, BibtexString> remaining)
        {
            // First remove this from the "remaining" list so it can't cause problem with circular refs:
            remaining.Remove(bs.getName());

            // Then we go through the string looking for references to other strings. If we find references
            // to strings that we will write, but still haven't, we write those before proceeding. This ensures
            // that the string order will be acceptable for BibTeX.
            string content = bs.getContent();
            Match m;
            while ((m = refPat.Match(content)).Success)
            {
                // TODO: this was group(1)
                string foundLabel = m.Groups[0].Value;
                int restIndex = content.IndexOf(foundLabel) + foundLabel.Length;
                content = content.Substring(restIndex);
                Object referred = remaining[foundLabel.Substring(1, foundLabel.Length - 1)];
                // If the label we found exists as a key in the "remaining" Map, we go on and write it now:
                if (referred != null)
                    writeString(fw, (BibtexString)referred, remaining);
            }

            fw.Write("@STRING{" + bs.getName() + " = ");
            if (!bs.getContent().Equals(""))
            {
                try
                {
                    string formatted = (new LatexFieldFormatter()).format(bs.getContent(), Globals.BIBTEX_STRING);
                    fw.Write(formatted);
                }
                catch (ArgumentException)
                {
                    throw new ArgumentException(
                            Globals.lang("The # character is not allowed in BibTeX strings unless escaped as in '\\#'.") + "\n" +
                            Globals.lang("Before saving, please edit any strings containing the # character."));
                }

            }
            else
                fw.Write("{}");

            fw.Write("}" + Environment.NewLine + Environment.NewLine);
        }