/**
 * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.liferay.blade.upgrade.liferay70;

import aQute.lib.io.IO;

import com.liferay.knowledge.base.markdown.converter.factory.MarkdownConverterFactoryUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.select.Elements;

public class MarkdownParser {

	private final static Map<String, Map<String, String>> _markdowns = new HashMap<>();

	public static Map<String, String> parse(String fileName) {
		Map<String, String> retval = _markdowns.get(fileName);

		if (retval == null) {
			try {
				final String markdown = new String(IO.read(MarkdownParser.class.getResourceAsStream(fileName)));
				final String html = MarkdownConverterFactoryUtil.create().convert(markdown);

				Map<String, String> sections = parseHtml(html);

				_markdowns.put(fileName, sections);

				retval = sections;
			} catch (IOException e) {
			}
		}

		return retval;
	}

	private static Map<String, String> parseHtml(String html) {
		Map<String, String> retval = new HashMap<>();

		Document document = Jsoup.parse(html);
		Elements elements = document.select("a[href] > h3");

		for (Element h3 : elements)  {
			Element a = h3.parent();
			int index = a.siblingIndex();
			List<Node> siblings = a.siblingNodes();

			StringBuilder sb = new StringBuilder();
			List<Node> interesting = new ArrayList<>();

			for (int i = index; i < siblings.size(); i++) {
				Node sibling = siblings.get(i);

				if (sibling.toString().startsWith("<hr")) {
					break;
				}
				else {
					interesting.add(sibling);
				}
			}

			for (Node node : interesting) {
				sb.append(node.toString());
			}

			String href = a.attr("href");

			retval.put(href, sb.toString());
		}

		return retval;
	}

	public static String getSection(String fileName, String sectionKey) {
		String retval = null;

		if (sectionKey.equals("#legacy")) {
			retval = "#legacy";
		}
		else {
			final Map<String, String> sections = parse(fileName);

			if (sections != null) {
				retval = sections.get(sectionKey);
			}
		}

		return retval;
	}
}

@@ -914,16 +914,80 @@
 til.Map;
+%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0Aimport
@@ -2519,38 +2519,274 @@
 %0A%09%09%09
-retval.put(href, sb.toString()
+String content = sb.toString();%0A%09%09%09String find = %22%5C%5C%5B%5C%5C%5D%5C%5C(id=%5B%5E%5C%5Cs%5D+?%5C%5C)%22;%0A%09%09%09String replace = %22%22;%0A%09%09%09Pattern pattern = Pattern.compile(find);%0A%09%09%09Matcher matcher = pattern.matcher(content);%0A%09%09%09String output = matcher.replaceAll(replace);%0A%0A%09%09%09retval.put(href, output
 );%0A%09

remove ids in html