import re


def remove_duplicated_whitespaces(text):
	"""Removes duplicated whitespaces in the text, and also
		remove whitespace at the beginning and end of lines.

	Args:
		text (string): markdown text that is going to be processed.

	Returns:
		string: text once it is processed.
	"""
	tab_regex = re.compile(r'\t+', re.MULTILINE | re.UNICODE)
	blank_space_regex = re.compile(r' +', re.MULTILINE | re.UNICODE)
	beginning_blank_space = re.compile(r'^ +', re.MULTILINE | re.UNICODE)
	ending_blank_space = re.compile(r' +$', re.MULTILINE | re.UNICODE)
	processed_text = tab_regex.sub(r' ', text)
	processed_text = blank_space_regex.sub(r' ', processed_text)
	processed_text = beginning_blank_space.sub(r'', processed_text)
	processed_text = ending_blank_space.sub(r'', processed_text)
	return processed_text