Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ec9343be0a669a82a6fe5b2a9e2030f7cb7a7af3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*******************************************************************************
 * Copyright (c) 2012 Stefan Seelmann and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Stefan Seelmann - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.wikitext.markdown.core.block;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.mylyn.wikitext.core.parser.Attributes;
import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder.BlockType;
import org.eclipse.mylyn.wikitext.core.parser.markup.Block;

/**
 * Markdown code block.
 * 
 * @author Stefan Seelmann
 */
public class CodeBlock extends Block {

	private static final Pattern startPattern = Pattern.compile("(?: {4}|\\t)((?: {4}|\\t)*)(.*)"); //$NON-NLS-1$

	private int blockLineCount = 0;

	@Override
	public boolean canStart(String line, int lineOffset) {
		if (lineOffset == 0) {
			return startPattern.matcher(line).matches();
		} else {
			return false;
		}
	}

	@Override
	protected int processLineContent(String line, int offset) {

		// start of block
		if (blockLineCount == 0) {
			builder.beginBlock(BlockType.PREFORMATTED, new Attributes());
			builder.beginBlock(BlockType.CODE, new Attributes());
		}

		// extract the content
		Matcher matcher = startPattern.matcher(line);
		if (!matcher.matches()) {
			setClosed(true);
			return 0;
		}
		String intent = matcher.group(1);
		String content = matcher.group(2);

		// next line, does not convert to line break
		if (blockLineCount > 0) {
			builder.characters("\n"); //$NON-NLS-1$
		}

		// emit, replace intention tabs by 4 spaces, encode ampersands (&) and angle brackets (< and >)
		if (intent != null) {
			builder.characters(intent.replace("\t", "    ")); //$NON-NLS-1$ //$NON-NLS-2$
		}
		builder.characters(content);

		blockLineCount++;
		return -1;
	}

	@Override
	public void setClosed(boolean closed) {
		if (closed && !isClosed()) {
			builder.endBlock();
			builder.endBlock();
		}
		super.setClosed(closed);
	}
}

Back to the top