/// <summary> /// Gets the vote partitions of a plan. /// </summary> /// <param name="lines">The lines of a vote plan.</param> /// <param name="partitionMode">The partition mode being used.</param> /// <param name="author">The author of the post.</param> /// <returns>Returns the vote partitioned appropriately.</returns> private List <string> GetVotePartitionsFromPlan(IEnumerable <string> lines, PartitionMode partitionMode, string author) { switch (partitionMode) { case PartitionMode.None: // No partitioning; no special treatment return(PartitionByNone(lines, author)); case PartitionMode.ByLine: // When partitioning by line, promote the plan first. // The label line can be discarded, and the others treated as less indented. return(PartitionByLine(PromoteLines(lines), author)); case PartitionMode.ByLineTask: // When partitioning by line, promote the plan first. // The label line can be discarded, and the others treated as less indented. return(PartitionByLineTask(lines, author)); case PartitionMode.ByBlock: // Normal block partitioning means we don't partition plans. // They will end up as a single block for the regular vote to consume. return(PartitionByNone(lines, author)); case PartitionMode.ByBlockAll: // When partitioning by BlockAll, any plans are themselves partitioned by block (after promotion). // Make sure to preserve the task from the main line on the resulting blocks. string planTask = VoteString.GetVoteTask(lines.First()); var blocks = PartitionByBlock(PromoteLines(lines), author); return(ApplyTaskToBlocks(blocks, planTask)); default: throw new ArgumentException($"Unknown partition mode: {partitionMode}"); } }
/// <summary> /// Add the specified task to all the provided blocks, if they don't /// already have a task. /// </summary> /// <param name="blocks">A list of vote blocks.</param> /// <param name="planTask">A task name to apply. If no name is provided, no changes are made.</param> /// <returns>Returns the vote blocks with the task applied.</returns> private static List <string> ApplyTaskToBlocks(List <string> blocks, string planTask) { if (blocks == null) { throw new ArgumentNullException(nameof(blocks)); } if (string.IsNullOrEmpty(planTask)) { return(blocks); } List <string> results = new List <string>(); foreach (var block in blocks) { if (VoteString.GetVoteTask(block).Length == 0) { string rep = VoteString.ModifyVoteLine(block, task: planTask, byPartition: true); results.Add(rep); } else { results.Add(block); } } return(results); }
/// <summary> /// Filters the plans by task. /// </summary> /// <param name="plans">The plans.</param> /// <param name="taskFilter">The task filter.</param> /// <returns>Returns the plans after filtering with the task filter.</returns> private static List <List <string> > FilterPlansByTask(List <List <string> > plans, IQuest quest) { if (!quest.UseCustomTaskFilters) { return(plans); } // Include lines where the task filter matches var filtered = plans.Where(p => quest.TaskFilter.Match(VoteString.GetVoteTask(p.First()))); return(filtered.ToList()); }
/// <summary> /// Partition a vote by line, but carry any task on parent lines down /// to child lines. /// </summary> /// <param name="lines">The lines of the vote.</param> /// <param name="author">The author of the vote, for use in determining /// valid referrals.</param> /// <returns>Returns a list of partitioned vote lines.</returns> private List <string> PartitionByLineTask(IEnumerable <string> lines, string author) { List <string> partitions = new List <string>(); List <string> referralVotes = new List <string>(); string parentTask = string.Empty; foreach (string line in lines) { // If someone copy/pasted a vote with a referral at the top (eg: self-named plan), // skip the copy/pasted section. if (referralVotes.Any()) { if (Agnostic.StringComparer.Equals(line, referralVotes.First())) { referralVotes = referralVotes.Skip(1).ToList(); continue; } referralVotes.Clear(); } referralVotes = VoteCounter.GetVotesFromReference(line, author); if (referralVotes.Any()) { partitions.AddRange(referralVotes); if (Agnostic.StringComparer.Equals(line, referralVotes.First())) { referralVotes = referralVotes.Skip(1).ToList(); continue; } } else { string taskedLine = line; if (string.IsNullOrEmpty(VoteString.GetVotePrefix(line))) { parentTask = VoteString.GetVoteTask(line); } else if (string.IsNullOrEmpty(VoteString.GetVoteTask(line))) { taskedLine = VoteString.ModifyVoteLine(line, task: parentTask); } partitions.Add(taskedLine + "\r\n"); } } return(partitions); }
/// <summary> /// Filters the votes by task. /// </summary> /// <param name="lines">The lines.</param> /// <param name="taskFilter">The task filter.</param> /// <returns>Returns the votes after filtering with the task filter.</returns> private static List <string> FilterVotesByTask(List <string> lines, IQuest quest) { if (!quest.UseCustomTaskFilters) { return(lines); } List <string> results = new List <string>(); foreach (var line in lines) { string firstLine = line.GetFirstLine(); string task = VoteString.GetVoteTask(firstLine); bool check = quest.TaskFilter.Match(task); if (check) { results.Add(line); } } return(results); }