コード例 #1
0
ファイル: MarkupParser.cpp プロジェクト: RAZVOR/haiku
void
MarkupParser::_ParseText(const BString& text)
{
	int32 start = 0;
	int32 offset = 0;

	int32 charCount = text.CountChars();
	const char* c = text.String();

	while (offset <= charCount) {
		uint32 nextChar = UTF8ToCharCode(&c);

		switch (nextChar) {
			case '\n':
			case '\0':
				_CopySpan(text, start, offset);
				_FinishParagraph();
				start = offset + 1;
				break;

			case '\'':
				if (offset + 2 < charCount && c[0] == '\'') {
					int32 tickCount = 2;
					if (c[1] == '\'')
						tickCount = 3;

					// Copy previous span using current style, excluding the
					// ticks.
					_CopySpan(text, start, offset);

					if (tickCount == 2)
						_ToggleStyle(fItalicStyle);
					else if (tickCount == 3)
						_ToggleStyle(fBoldStyle);

					// Don't include the ticks in the next span.
					offset += tickCount - 1;
					start = offset + 1;
					c += tickCount - 1;
				}
				break;

			case '=':
				// Detect headings
				if (offset == start
					&& fCurrentParagraph.IsEmpty()
					&& offset + 2 < charCount
					&& c[0] == '=' && c[1] == ' ') {

					fCurrentParagraph.SetStyle(fHeadingParagraphStyle);
					fCurrentCharacterStyle = &fHeadingStyle;

					offset += 2;
					c += 2;

					start = offset + 1;
				} else if (offset > start
					&& offset + 2 < charCount
					&& c[0] == '=' && c[1] == '\n') {

					_CopySpan(text, start, offset - 1);
					_FinishParagraph();

					offset += 2;
					c += 2;

					start = offset + 1;
				}
				break;

			case ' ':
				// Detect bullets at line starts
				if (offset == start
					&& fCurrentParagraph.IsEmpty()
					&& offset + 2 < charCount
					&& c[0] == '*' && c[1] == ' ') {

					fCurrentParagraph.SetStyle(fBulletStyle);

					offset += 2;
					c += 2;

					start = offset + 1;
				}
				break;

			default:
				break;

		}
		offset++;
	}
}
コード例 #2
0
ファイル: MarkupParser.cpp プロジェクト: AmirAbrams/haiku
void
MarkupParser::_ParseText(const BString& text)
{
	int32 start = 0;
	int32 offset = 0;

	int32 charCount = text.CountChars();
	const char* c = text.String();

	while (offset <= charCount) {
		uint32 nextChar = UTF8ToCharCode(&c);

		switch (nextChar) {
// Requires two line-breaks to start a new paragraph, unles the current
// paragraph is already considered a bullet list item. Doesn't work well
// with current set of packages.
//			case '\n':
//				_CopySpan(text, start, offset);
//				if (offset + 1 < charCount && c[0] == '\n') {
//					_FinishParagraph();
//					offset += 1;
//					c += 1;
//				} else if (fCurrentParagraph.Style() == fBulletStyle) {
//					_FinishParagraph();
//				}
//				start = offset + 1;
//				break;

			case '\n':
				_CopySpan(text, start, offset);
				if (offset > 0 && c[-1] != ' ')
					_FinishParagraph(offset >= charCount);
				start = offset + 1;
				break;

			case '\0':
				_CopySpan(text, start, offset);
				_FinishParagraph(true);
				start = offset + 1;
				break;

			case '\'':
				if (offset + 2 < charCount && c[0] == '\'') {
					int32 tickCount = 2;
					if (c[1] == '\'')
						tickCount = 3;

					// Copy previous span using current style, excluding the
					// ticks.
					_CopySpan(text, start, offset);

					if (tickCount == 2)
						_ToggleStyle(fItalicStyle);
					else if (tickCount == 3)
						_ToggleStyle(fBoldStyle);

					// Don't include the ticks in the next span.
					offset += tickCount - 1;
					start = offset + 1;
					c += tickCount - 1;
				}
				break;

			case '=':
				// Detect headings
				if (offset == start
					&& fCurrentParagraph.IsEmpty()
					&& offset + 2 < charCount
					&& c[0] == '=' && c[1] == ' ') {

					fCurrentParagraph.SetStyle(fHeadingParagraphStyle);
					fCurrentCharacterStyle = &fHeadingStyle;

					offset += 2;
					c += 2;

					start = offset + 1;
				} else if (offset > start
					&& offset + 2 < charCount
					&& c[0] == '=' && c[1] == '\n') {

					_CopySpan(text, start, offset - 1);

					offset += 2;
					c += 2;

					_FinishParagraph(offset >= charCount);

					start = offset + 1;
				}
				break;

			case ' ':
				// Detect bullets at line starts (preceeding space)
				if (offset == start
					&& fCurrentParagraph.IsEmpty()
					&& offset + 2 < charCount
					&& (c[0] == '*' || c[0] == '-') && c[1] == ' ') {

					fCurrentParagraph.SetStyle(fBulletStyle);

					offset += 2;
					c += 2;

					start = offset + 1;
				}
				break;

			case '*':
			case '-':
				// Detect bullets at line starts (no preceeding space)
				if (offset == start
					&& fCurrentParagraph.IsEmpty()
					&& offset + 1 < charCount
					&& c[0] == ' ') {

					fCurrentParagraph.SetStyle(fBulletStyle);

					offset += 1;
					c += 1;

					start = offset + 1;
				}
				break;

			default:
				break;

		}
		offset++;
	}
}