internal static CommentLine CreateNew(Entry entry, CommentLine.Kinds kind, int?index, bool isContinuation, IO.TextLine addRelativeTo, bool addRelativeToBefore, string text) { if (entry == null) { throw new ArgumentNullException("entry"); } if (addRelativeTo == null) { throw new ArgumentNullException("addRelativeTo"); } int insertAt = entry.File.TextFile.Lines.IndexOf(addRelativeTo); if (insertAt < 0) { throw new ArgumentOutOfRangeException("addRelativeTo"); } if (!addRelativeToBefore) { insertAt++; } IO.TextLine textLine = new IO.TextLine(BuildTextFileData(kind, index, isContinuation, text)); entry.File.TextFile.Lines.Insert(insertAt, textLine); CommentLine result; if (index.HasValue) { result = new IndexedCommentLine(entry.File, textLine, isContinuation, kind, text, index.Value); } else { result = new CommentLine(entry.File, textLine, isContinuation, kind, text); } entry.LineAdded(result); return(result); }
public static Line ParseLine(POFile file, IO.TextLine source, Line previous) { if (source.Value[0] == '#') { return(CommentLine.Parse(file, source, previous)); } else { return(DataLine.Parse(file, source, previous)); } }
public static CommentLine Parse(POFile file, IO.TextLine source, Line previous) { Match match; bool isContinuation; CommentLine.Kinds?kind; string text; int?index; string sourceValue = source.Value; if ((match = CommentLine.RxContinuation_PreviousUntraslated.Match(sourceValue)).Success) { if (previous == null) { throw new Exception("Comment of kind 'previous-untranslated' continuation at block start."); } if (previous.Type != Line.Types.Comment) { throw new Exception("Comment of kind 'previous-untranslated' continuation after a non-comment line."); } CommentLine prevComment = (CommentLine)previous; switch (prevComment._kind) { case CommentLine.Kinds.PreviousUntraslated_Context: case CommentLine.Kinds.PreviousUntraslated_ID: case CommentLine.Kinds.PreviousUntraslated_IDPlural: break; default: throw new Exception(string.Format("Comment of kind 'previous-untranslated' continuation after a comment of kind '{0}'.", prevComment._kind)); } isContinuation = true; kind = prevComment._kind; text = match.Groups["text"].Value; Line.IIndexedLine idx = prevComment as Line.IIndexedLine; index = (idx != null) ? idx.Index : (int?)null; } else if ((match = CommentLine.RxContinuation_Removed.Match(sourceValue)).Success) { if (previous == null) { throw new Exception("Comment of kind 'removed' continuation at block start."); } if (previous.Type != Line.Types.Comment) { throw new Exception("Comment of kind 'removed' continuation after a non-comment line."); } CommentLine prevComment = (CommentLine)previous; switch (prevComment._kind) { case CommentLine.Kinds.Removed_Context: case CommentLine.Kinds.Removed_ID: case CommentLine.Kinds.Removed_IDPlural: case CommentLine.Kinds.Removed_Translated: case CommentLine.Kinds.Removed_TranslatedIndexed: break; default: throw new Exception(string.Format("Comment of kind 'removed' continuation after a comment of kind '{0}'.", prevComment._kind)); } isContinuation = true; kind = prevComment._kind; text = match.Groups["text"].Value; Line.IIndexedLine idx = prevComment as Line.IIndexedLine; index = (idx != null) ? idx.Index : (int?)null; } else { isContinuation = false; kind = null; text = null; index = null; foreach (KeyValuePair <CommentLine.Kinds, Regex> kv in CommentLine.RxFindKind) { match = kv.Value.Match(sourceValue); if (!match.Success) { continue; } kind = kv.Key; text = match.Groups["text"].Value; index = (match.Groups["index"].Value.Length > 0) ? int.Parse(match.Groups["index"].Value) : (int?)null; break; } } if (index.HasValue) { return(new IndexedCommentLine(file, source, isContinuation, kind.Value, text, index.Value)); } else { return(new CommentLine(file, source, isContinuation, kind.Value, text)); } }
internal IndexedCommentLine(POFile file, IO.TextLine source, bool isContinuation, CommentLine.Kinds kind, string text, int index) : base(file, source, isContinuation, kind, text) { this._index = index; }
protected CommentLine(POFile file, IO.TextLine source, bool isContinuation, CommentLine.Kinds kind, string text) : base(file, source, Line.Types.Comment, isContinuation, text) { this._kind = kind; }
private static string BuildTextFileData(CommentLine.Kinds kind, int? index, bool isContinuation, string text) { if (isContinuation) { switch (kind) { case CommentLine.Kinds.PreviousUntraslated_Context: case CommentLine.Kinds.PreviousUntraslated_ID: case CommentLine.Kinds.PreviousUntraslated_IDPlural: return string.Format("#| \"{0}\"", text); case CommentLine.Kinds.Removed_Context: case CommentLine.Kinds.Removed_ID: case CommentLine.Kinds.Removed_IDPlural: case CommentLine.Kinds.Removed_Translated: case CommentLine.Kinds.Removed_TranslatedIndexed: return string.Format("#~ \"{0}\"", text); default: throw new NotImplementedException(); } } else { switch (kind) { case CommentLine.Kinds.TranslatorComment: return string.Format("# {0}", text); case CommentLine.Kinds.ExtractedComment: return string.Format("#. {0}", text); case CommentLine.Kinds.Reference: return string.Format("#: {0}", text); case CommentLine.Kinds.Flags: return string.Format("#, {0}", text); case CommentLine.Kinds.PreviousUntraslated_Context: return string.Format("#| msgctxt \"{0}\"", text); case CommentLine.Kinds.PreviousUntraslated_ID: return string.Format("#| msgid \"{0}\"", text); case CommentLine.Kinds.PreviousUntraslated_IDPlural: return string.Format("#| msgid_plural \"{0}\"", text); case CommentLine.Kinds.Removed_Context: return string.Format("#~ msgctxt \"{0}\"", text); case CommentLine.Kinds.Removed_ID: return string.Format("#~ msgid \"{0}\"", text); case CommentLine.Kinds.Removed_IDPlural: return string.Format("#~ msgid_plural \"{0}\"", text); case CommentLine.Kinds.Removed_Translated: return string.Format("#~ msgstr \"{0}\"", text); case CommentLine.Kinds.Removed_TranslatedIndexed: return string.Format("#~ msgstr[{0}] \"{1}\"", index.Value, text); default: throw new NotImplementedException(); } } }
internal static CommentLine CreateNew(Entry entry, CommentLine.Kinds kind, int? index, bool isContinuation, IO.TextLine addRelativeTo, bool addRelativeToBefore, string text) { if (entry == null) throw new ArgumentNullException("entry"); if (addRelativeTo == null) throw new ArgumentNullException("addRelativeTo"); int insertAt = entry.File.TextFile.Lines.IndexOf(addRelativeTo); if (insertAt < 0) throw new ArgumentOutOfRangeException("addRelativeTo"); if (!addRelativeToBefore) insertAt++; IO.TextLine textLine = new IO.TextLine(BuildTextFileData(kind, index, isContinuation, text)); entry.File.TextFile.Lines.Insert(insertAt, textLine); CommentLine result; if (index.HasValue) result = new IndexedCommentLine(entry.File, textLine, isContinuation, kind, text, index.Value); else result = new CommentLine(entry.File, textLine, isContinuation, kind, text); entry.LineAdded(result); return result; }