Processing Ajax...

Title

Message

Confirm

Confirm

Confirm

Confirm

Are you sure you want to delete this item?

Confirm

Are you sure you want to delete this item?

Combine Multi-Line Text into One Line

Description
This macro removes the line breaks from the copied text and combines everything onto one line.
Language
C#.net
Minimum Version
Created By
Thomas Malloch (BFS)
Contributors
-
Date Created
Feb 9, 2023
Date Last Modified
Feb 9, 2023

Macro Code

using System;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
public static class ClipboardFusionHelper
{
	public static string ProcessText(string text)
	{
		//make sure all of the line breaks are the same type & remove tabs
		// \0 = Null
		// \a = Alert
		// \b = Backspace
		// \f = Form feed
		// \n = New line
		// \r = Carriage return
		// \t = Horizontal tab
		// \v = Vertical tab
		text = text.Replace(Environment.NewLine, "\n")
				.Replace("\r", "\n")				
				.Replace("\0", "")
				.Replace("\a", "")
				.Replace("\b", "")
				.Replace("\f", "")				
				.Replace("\t", "")
				.Replace("\v", "");
		
		// replace instances of multiple line breaks with just one line break
        text = Regex.Replace(text, @"\n{2,}", "\n", RegexOptions.Multiline).Trim();	
		
		// replace instances of multiple spaces with just one space
        text = Regex.Replace(text, @"\s{2,}", " ", RegexOptions.Multiline).Trim();		
		
		//this will let us quickly build the string to return
		StringBuilder builder = new StringBuilder();
		
		//split the text up into its separate lines
		foreach(string line in text.Split(new char[]{'\n'}, StringSplitOptions.RemoveEmptyEntries))
		{
			//this is probably redundant since we are removing empty entries
			//if the line variable is null or empty, continue to the next line
			if(string.IsNullOrEmpty(line))
				continue;
				
			//if the line is blank, ignore it
			if(IsLineOnlyWhiteSpace(line))
				continue;
			
			// add a space to the builder if we already have some text in it, and the last character wasn't a dash
			if((builder.Length > 0) &&
				(builder[builder.Length - 1] != '-'))
			{
				builder.Append(" ");
			}
				
			//add the line to the StringBuilder
			builder.Append(line);
		}
		
		//paste the StringBuilder
		BFS.Clipboard.PasteText(builder.ToString());
		
		//since we already set the clipboard with the PasteText function, there's no need to set it again
		return null;
	}
	
	//this is a set of blank characters
	private static readonly Dictionary<char, int> WhiteSpaceChars = new Dictionary<char, int>
	{
		{' ', 0},
		{'\x0085', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{' ', 0},
		{'\x200B', 0},
		{' ', 0},
		{'\xFEFF', 0},
		{'\v', 0},
		{'\f', 0},
		{'\x2028', 0},
		{'\x2029',  0},
	};
	
	//this function returns true if a line is composed of only whitespace or blank characters
	private static bool IsLineOnlyWhiteSpace(string line)
	{
		foreach(char c in line)
		{	
			if(!WhiteSpaceChars.ContainsKey(c))
				return false;
		}
		
		return true;
	}
}