WEB-7515 documentation for required module

This commit is contained in:
Sergey Simonchik
2013-07-12 19:18:46 +04:00
parent 5dd900e015
commit e037fac766
4 changed files with 131 additions and 0 deletions

View File

@@ -204,6 +204,7 @@ libraryLicense(name: "JSch", libraryName: "JSch", version: "0.1.49", license: "B
libraryLicense(name: "JUnit", libraryName: "JUnit3", version: "3.8.1", license: "CPL 1.0", url: "http://junit.org/")
libraryLicense(name: "JUnit", libraryName: "JUnit4", version: "4.8", license: "CPL 1.0", url: "http://junit.org/")
libraryLicense(name: "Log4j", libraryName: "Log4J", version: "1.2.17", license: "Apache 2.0", url: "http://logging.apache.org/log4j/1.2/index.html", licenseUrl: "http://logging.apache.org/license.html")
libraryLicense(name: "markdownj", libraryName: "markdownj", version: "0.4.2", license: "New BSD", url: "https://code.google.com/p/markdownj/", licenseUrl: "http://opensource.org/licenses/BSD-3-Clause")
libraryLicense(name: "Maven", version: "2.2.1", license: "Apache 2.0", url: "http://maven.apache.org/", licenseUrl: "http://maven.apache.org/license.html")
libraryLicense(name: "plexus-util", version: "2.0.6", license: "Apache 2.0", url: "http://maven.apache.org/", libraryNames:['plexus-utils-2.0.6.jar'], licenseUrl: "http://apache.org/licenses/LICENSE-2.0")
libraryLicense(name: "aether-api", version: "1.11", libraryNames: ["aether-api-1.11.jar"], license: "Apache 2.0", url: "http://maven.apache.org/", licenseUrl: "http://maven.apache.org/license.html")

Binary file not shown.

View File

@@ -25,6 +25,7 @@ junit-4.10.jar
log4j.jar
jgoodies-common-1.2.1.jar
jgoodies-looks-2.4.2.jar
markdownj-core-0.4.2-SNAPSHOT.jar
microba.jar
miglayout-swing.jar
nanoxml-2.2.3.jar

View File

@@ -0,0 +1,129 @@
package com.intellij.util.text;
import com.intellij.openapi.util.TextRange;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* @author Sergey Simonchik
*/
public class MarkdownUtil {
private MarkdownUtil() {}
/**
* Replaces headers in markdown with HTML.
* Unfortunately, library used for markdown processing
* <a href="https://code.google.com/p/markdownj">markdownj</a>
* doesn't support that.
*/
public static void replaceHeaders(List<String> lines) {
for (int i = 0; i < lines.size(); i++) {
String line = lines.get(i);
int ind = 0;
while (ind < line.length() && line.charAt(ind) == '#') {
ind++;
}
if (ind < line.length() && line.charAt(ind) == ' ') {
if (0 < ind && ind <= 9) {
int endInd = line.length() - 1;
while (endInd >= 0 && line.charAt(endInd) == '#') {
endInd--;
}
line = line.substring(ind + 1, endInd + 1);
line = "<h" + ind + ">" + line + "</h" + ind + ">";
lines.set(i, line);
}
}
}
}
public static void removeImages(@NotNull List<String> lines) {
for (int i = 0; i < lines.size(); i++) {
String newText = removeAllImages(lines.get(i));
lines.set(i, newText);
}
}
@NotNull
private static String removeAllImages(@NotNull String text) {
int n = text.length();
List<TextRange> intervals = null;
int i = 0;
while (i < n) {
int imageEndIndex = findImageEndIndexInclusive(text, i);
if (imageEndIndex != -1) {
TextRange linkRange = findEnclosingLink(text, i, imageEndIndex);
if (intervals == null) {
intervals = new ArrayList<TextRange>(1);
}
final TextRange range;
if (linkRange != null) {
range = linkRange;
}
else {
range = new TextRange(i, imageEndIndex);
}
intervals.add(range);
i = range.getEndOffset();
}
i++;
}
if (intervals == null) {
return text;
}
StringBuilder buf = new StringBuilder(text);
for (int intervalInd = intervals.size() - 1; intervalInd >= 0; intervalInd--) {
TextRange range = intervals.get(intervalInd);
buf.delete(range.getStartOffset(), range.getEndOffset() + 1);
}
return buf.toString();
}
private static int findImageEndIndexInclusive(@NotNull String text, int imageStartIndex) {
int n = text.length();
if (text.charAt(imageStartIndex) == '!'
&& imageStartIndex + 1 < n
&& text.charAt(imageStartIndex + 1) == '[') {
int i = imageStartIndex + 2;
while (i < n && text.charAt(i) != ']') {
i++;
}
if (i < n && i + 1 < n && text.charAt(i + 1) == '(') {
i += 2;
while (i < n && text.charAt(i) != ')') {
i++;
}
if (i < n) {
return i;
}
}
}
return -1;
}
@Nullable
private static TextRange findEnclosingLink(@NotNull String text,
int imageStartIndInc,
int imageEndIndInc) {
int linkStartIndInc = imageStartIndInc - 1;
if (linkStartIndInc >= 0 && text.charAt(linkStartIndInc) == '[') {
int n = text.length();
int i = imageEndIndInc + 1;
if (i + 1 <= n && text.charAt(i) == ']' && text.charAt(i + 1) == '(') {
i += 2;
while (i < n && text.charAt(i) != ')') {
i++;
}
if (i < n) {
return new TextRange(linkStartIndInc, i);
}
}
}
return null;
}
}