<commit_before>package org.sagebionetworks.web.server.markdownparser;

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

import org.sagebionetworks.web.client.widget.entity.registration.WidgetEncodingUtil;
import org.sagebionetworks.web.client.widget.entity.registration.WidgetConstants;
import org.sagebionetworks.web.shared.WebConstants;

public class ReferenceParser extends BasicMarkdownElementParser {
	Pattern p1= Pattern.compile(MarkdownRegExConstants.REFERENCE_REGEX);
	ArrayList<String> footnotes;
	int footnoteNumber;
	
	@Override
	public void reset() {
		footnotes = new ArrayList<String>();
		footnoteNumber = 1;
	}

	@Override
	public void processLine(MarkdownElements line) {
		String input = line.getMarkdown();
		Matcher m = p1.matcher(input);
		StringBuffer sb = new StringBuffer();
		while(m.find()) {
			//Store reference text and insert extra parameter for the renderer to link to
			int lenOfSyntax = "${reference?text=".length();
			footnotes.add(input.substring(m.start() + lenOfSyntax, m.end() - 1));
			String updated = input.substring(m.start(), m.end() - 1) + 
				"&" + WidgetConstants.INLINE_WIDGET_KEY + "=true&" + WidgetConstants.REFERENCE_FOOTNOTE_KEY + "=" + footnoteNumber + "}";
			updated = Matcher.quoteReplacement(updated);	//Escapes the replacement string for appendReplacement
			m.appendReplacement(sb, updated);
			footnoteNumber++;
		}
		m.appendTail(sb);
		line.updateMarkdown(sb.toString());
	}
	
	@Override
	public void completeParse(StringBuilder html) {
		for(int i = 0; i < footnotes.size(); i++) {
			String text = WidgetEncodingUtil.decodeValue(footnotes.get(i));
			String footnote = "<p id=\"" + WebConstants.FOOTNOTE_ID_WIDGET_PREFIX + (i + 1) + "\">[" + (i + 1) + "] " + text + "</p>";
			html.append(footnote);
		}
	}
}
<commit_msg>SWC-453 Added description on how to make widgets inline<commit_after>package org.sagebionetworks.web.server.markdownparser;

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

import org.sagebionetworks.web.client.widget.entity.registration.WidgetEncodingUtil;
import org.sagebionetworks.web.client.widget.entity.registration.WidgetConstants;
import org.sagebionetworks.web.shared.WebConstants;

public class ReferenceParser extends BasicMarkdownElementParser {
	Pattern p1= Pattern.compile(MarkdownRegExConstants.REFERENCE_REGEX);
	ArrayList<String> footnotes;
	int footnoteNumber;
	
	@Override
	public void reset() {
		footnotes = new ArrayList<String>();
		footnoteNumber = 1;
	}

	@Override
	public void processLine(MarkdownElements line) {
		String input = line.getMarkdown();
		Matcher m = p1.matcher(input);
		StringBuffer sb = new StringBuffer();
		while(m.find()) {
			//Store reference text
			int lenOfSyntax = "${reference?text=".length();
			footnotes.add(input.substring(m.start() + lenOfSyntax, m.end() - 1));
			
			/*
			 * Insert any extra parameters/values by appending to the widget's original expression/parameters
			 * (Don't forget the closing "}") Use as a replacement string:
			 * 
			 * To make widgets inline add:
			 * &inlineWidget=true (this will render the widget inline)
			 * 
			 * Other:
			 * &footnoteId=# (this id tells the renderer which element to link to)
			 */
			String updated = input.substring(m.start(), m.end() - 1) + 
				"&" + WidgetConstants.INLINE_WIDGET_KEY + "=true&" + WidgetConstants.REFERENCE_FOOTNOTE_KEY + "=" + footnoteNumber + "}";
			updated = Matcher.quoteReplacement(updated);	//Escapes the replacement string for appendReplacement
			m.appendReplacement(sb, updated);
			footnoteNumber++;
		}
		m.appendTail(sb);
		line.updateMarkdown(sb.toString());
	}
	
	@Override
	public void completeParse(StringBuilder html) {
		for(int i = 0; i < footnotes.size(); i++) {
			String text = WidgetEncodingUtil.decodeValue(footnotes.get(i));
			String footnote = "<p id=\"" + WebConstants.FOOTNOTE_ID_WIDGET_PREFIX + (i + 1) + "\">[" + (i + 1) + "] " + text + "</p>";
			html.append(footnote);
		}
	}
}

{'fraction_non_alphanumeric': 0.13101736972704714, 'fraction_numerical': 0.004714640198511167, 'mean_word_length': 4.583102493074792, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4157329', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1269, 'n_words': 280}