use of org.eclipse.mylyn.wikitext.parser.MarkupParser in project mylyn.docs by eclipse.
the class MarkupToOPS method parse.
/**
* Parses the markup file and populates the publication with the result.
*
* @param ops
* the publication the content will be added to
* @param markupFile
* the WikiText markup file
* @return the temporary folder used for generating the HTML from markup
* @since 2.0
*/
public File parse(Publication ops, File markupFile) throws IOException, FileNotFoundException {
if (markupLanguage == null) {
// $NON-NLS-1$
throw new IllegalStateException("must set markupLanguage");
}
// Create a temporary working folder
// $NON-NLS-1$
File workingFolder = File.createTempFile("wikitext_", null);
if (workingFolder.delete() && workingFolder.mkdirs()) {
// $NON-NLS-1$
File htmlFile = new File(workingFolder.getAbsolutePath() + File.separator + "markup.html");
FileWriter out = new FileWriter(htmlFile);
HtmlDocumentBuilder builder = new HtmlDocumentBuilder(out) {
@Override
protected XmlStreamWriter createXmlStreamWriter(Writer out) {
return super.createFormattingXmlStreamWriter(out);
}
};
List<Item> stylesheets = ops.getItemsByMIMEType(Publication.MIMETYPE_CSS);
for (Item item : stylesheets) {
File file = new File(item.getFile());
Stylesheet css = new Stylesheet(file);
builder.addCssStylesheet(css);
}
// Make sure we get the correct XHTML header
builder.setEmitDtd(true);
builder.setHtmlDtd(// $NON-NLS-1$
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">");
builder.setXhtmlStrict(true);
MarkupParser markupParser = new MarkupParser();
markupParser.setBuilder(builder);
markupParser.setMarkupLanguage(markupLanguage);
markupParser.parse(new FileReader(markupFile));
ops.setGenerateToc(true);
ops.setIncludeReferencedResources(true);
Item item = ops.addItem(htmlFile);
item.setSourcePath(markupFile.getAbsolutePath());
}
return workingFolder;
}
use of org.eclipse.mylyn.wikitext.parser.MarkupParser in project mylyn.docs by eclipse.
the class MarkupToDitaTask method processFile.
private void processFile(MarkupLanguage markupLanguage, final File baseDir, final File source) throws BuildException {
// $NON-NLS-1$
log(MessageFormat.format(Messages.getString("MarkupToDitaTask.8"), source), Project.MSG_VERBOSE);
String markupContent = null;
String name = source.getName();
if (name.lastIndexOf('.') != -1) {
name = name.substring(0, name.lastIndexOf('.'));
}
// $NON-NLS-1$
File outputFile = new File(source.getParentFile(), filenameFormat.replace("$1", name));
if (!outputFile.exists() || overwrite || outputFile.lastModified() < source.lastModified()) {
if (markupContent == null) {
markupContent = readFully(source);
}
performValidation(source, markupContent);
OutlineItem outline = new OutlineParser(markupLanguage).parse(markupContent);
Writer writer;
try {
// $NON-NLS-1$
writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(outputFile)), "utf-8");
} catch (Exception e) {
throw new BuildException(// $NON-NLS-1$
MessageFormat.format(// $NON-NLS-1$
Messages.getString("MarkupToDitaTask.11"), // $NON-NLS-1$
outputFile, e.getMessage()), e);
}
try {
if (topicStrategy == BreakStrategy.NONE) {
DitaTopicDocumentBuilder builder = new DitaTopicDocumentBuilder(new DefaultXmlStreamWriter(writer), formatting);
builder.setRootTopicTitle(bookTitle);
MarkupParser parser = new MarkupParser();
parser.setMarkupLanguage(markupLanguage);
parser.setBuilder(builder);
if (topicDoctype != null) {
builder.setDoctype(topicDoctype);
}
builder.setFilename(outputFile.getName());
builder.setOutline(outline);
parser.parse(markupContent);
} else {
DitaBookMapDocumentBuilder builder = new DitaBookMapDocumentBuilder(formatting ? new FormattingXMLStreamWriter(new DefaultXmlStreamWriter(writer)) : new DefaultXmlStreamWriter(writer));
try {
builder.setFormattingDependencies(formatting);
MarkupParser parser = new MarkupParser();
parser.setMarkupLanguage(markupLanguage);
parser.setBuilder(builder);
builder.setBookTitle(bookTitle == null ? name : bookTitle);
if (doctype != null) {
builder.setDoctype(doctype);
}
if (topicDoctype != null) {
builder.setTopicDoctype(topicDoctype);
}
builder.setTargetFile(outputFile);
builder.setTopicFolder(topicFolder);
builder.setOutline(outline);
switch(topicStrategy) {
case FIRST:
if (!outline.getChildren().isEmpty()) {
builder.setTopicBreakLevel(outline.getChildren().get(0).getLevel());
} else {
builder.setTopicBreakLevel(1);
}
break;
case LEVEL1:
builder.setTopicBreakLevel(1);
break;
}
parser.parse(markupContent);
} finally {
try {
builder.close();
} catch (IOException e) {
throw new BuildException(MessageFormat.format(// $NON-NLS-1$
Messages.getString("MarkupToDitaTask.12"), // $NON-NLS-1$
outputFile, e.getMessage()), e);
}
}
}
} finally {
try {
writer.close();
} catch (Exception e) {
throw new BuildException(MessageFormat.format(// $NON-NLS-1$
Messages.getString("MarkupToDitaTask.12"), // $NON-NLS-1$
outputFile, e.getMessage()), e);
}
}
}
}
use of org.eclipse.mylyn.wikitext.parser.MarkupParser in project mylyn.docs by eclipse.
the class MarkupToHtmlTask method processFile.
/**
* process the file
*
* @param baseDir
* @param source
* @return the lightweight markup, or null if the file was not written
* @throws BuildException
*/
protected String processFile(MarkupLanguage markupLanguage, final File baseDir, final File source) throws BuildException {
// $NON-NLS-1$
log(MessageFormat.format(Messages.getString("MarkupToHtmlTask.14"), source), Project.MSG_VERBOSE);
String markupContent = null;
String name = source.getName();
if (name.lastIndexOf('.') != -1) {
name = name.substring(0, name.lastIndexOf('.'));
}
File htmlOutputFile = computeHtmlFile(source, name);
if (!htmlOutputFile.exists() || overwrite || htmlOutputFile.lastModified() < source.lastModified()) {
if (markupContent == null) {
markupContent = readFully(source);
}
performValidation(source, markupContent);
Writer writer;
try {
// $NON-NLS-1$
writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(htmlOutputFile)), "utf-8");
} catch (Exception e) {
throw new BuildException(MessageFormat.format(Messages.getString("MarkupToHtmlTask.16"), htmlOutputFile, e.getMessage()), // $NON-NLS-1$
e);
}
try {
HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer, formatOutput);
for (Stylesheet stylesheet : stylesheets) {
HtmlDocumentBuilder.Stylesheet builderStylesheet;
if (stylesheet.url != null) {
builderStylesheet = new HtmlDocumentBuilder.Stylesheet(stylesheet.url);
} else {
builderStylesheet = new HtmlDocumentBuilder.Stylesheet(stylesheet.file);
}
builder.addCssStylesheet(builderStylesheet);
if (!stylesheet.attributes.isEmpty()) {
for (Map.Entry<String, String> attr : stylesheet.attributes.entrySet()) {
builderStylesheet.getAttributes().put(attr.getKey(), attr.getValue());
}
}
}
builder.setTitle(title == null ? name : title);
builder.setEmitDtd(emitDoctype);
if (emitDoctype && htmlDoctype != null) {
builder.setHtmlDtd(htmlDoctype);
}
builder.setUseInlineStyles(useInlineCssStyles);
builder.setSuppressBuiltInStyles(suppressBuiltInCssStyles);
builder.setLinkRel(linkRel);
builder.setDefaultAbsoluteLinkTarget(defaultAbsoluteLinkTarget);
builder.setPrependImagePrefix(prependImagePrefix);
builder.setXhtmlStrict(xhtmlStrict);
builder.setCopyrightNotice(copyrightNotice);
builder.setHtmlFilenameFormat(htmlFilenameFormat);
SplittingStrategy splittingStrategy = multipleOutputFiles ? new DefaultSplittingStrategy() : new NoSplittingStrategy();
SplittingOutlineParser outlineParser = new SplittingOutlineParser();
outlineParser.setMarkupLanguage(markupLanguage.clone());
outlineParser.setSplittingStrategy(splittingStrategy);
SplitOutlineItem item = outlineParser.parse(markupContent);
item.setSplitTarget(htmlOutputFile.getName());
SplittingHtmlDocumentBuilder splittingBuilder = new SplittingHtmlDocumentBuilder();
splittingBuilder.setRootBuilder(builder);
splittingBuilder.setOutline(item);
splittingBuilder.setRootFile(htmlOutputFile);
splittingBuilder.setNavigationImages(navigationImages);
splittingBuilder.setFormatting(formatOutput);
MarkupParser parser = new MarkupParser();
parser.setMarkupLanguage(markupLanguage);
parser.setBuilder(splittingBuilder);
parser.parse(markupContent);
processed(markupContent, item, baseDir, source);
} finally {
try {
writer.close();
} catch (Exception e) {
throw new BuildException(MessageFormat.format(// $NON-NLS-1$
Messages.getString("MarkupToHtmlTask.17"), // $NON-NLS-1$
htmlOutputFile, e.getMessage()), e);
}
}
}
return markupContent;
}
use of org.eclipse.mylyn.wikitext.parser.MarkupParser in project mylyn.docs by eclipse.
the class HtmlTextPresentationParserTest method testSignificantWhitespaceLossBug236367.
/**
* Test for bug# 236367
*/
@Test
public void testSignificantWhitespaceLossBug236367() throws SAXException, IOException {
String html = new MarkupParser(new TextileLanguage()).parseToHtml("one *two* three *four* five *six* seven");
parser.parse(html);
String text = parser.getText();
assertTrue(text.contains("one two three four five six seven"));
}
use of org.eclipse.mylyn.wikitext.parser.MarkupParser in project mylyn.docs by eclipse.
the class ConvertMarkupToMarkup method handleFile.
@Override
protected void handleFile(ExecutionEvent event, IFile file, String name) throws ExecutionException {
MarkupLanguage targetMmarkupLanguage = ServiceLocator.getInstance().getMarkupLanguage(event.getParameter(PARAM_MARKUP_LANGUAGE));
// TODO: better way to get the file extension
// $NON-NLS-1$ //$NON-NLS-2$
String extension = targetMmarkupLanguage.getName().toLowerCase().replaceAll("\\W", "");
// $NON-NLS-1$
final IFile newFile = file.getParent().getFile(new Path(name + "." + extension));
if (newFile.exists()) {
if (!MessageDialog.openQuestion(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), NLS.bind(Messages.ConvertMarkupToMarkup_overwrite_file, targetMmarkupLanguage.getName()), NLS.bind(Messages.ConvertMarkupToMarkup_overwrite_file_detail, new Object[] { newFile.getFullPath() }))) {
return;
}
}
StringWriter writer = new StringWriter();
MarkupParser parser = new MarkupParser();
parser.setMarkupLanguage(markupLanguage);
parser.setBuilder(targetMmarkupLanguage.createDocumentBuilder(writer));
try {
String inputContent = IOUtil.readFully(file);
parser.parse(inputContent);
final String targetConent = writer.toString();
IRunnableWithProgress runnable = new IRunnableWithProgress() {
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
try {
if (newFile.exists()) {
// $NON-NLS-1$
newFile.setContents(// $NON-NLS-1$
new ByteArrayInputStream(targetConent.getBytes("utf-8")), // $NON-NLS-1$
false, // $NON-NLS-1$
true, monitor);
} else {
// $NON-NLS-1$
newFile.create(new ByteArrayInputStream(targetConent.getBytes("utf-8")), false, monitor);
}
// $NON-NLS-1$
newFile.setCharset("utf-8", monitor);
} catch (Exception e) {
throw new InvocationTargetException(e);
}
}
};
try {
PlatformUI.getWorkbench().getProgressService().busyCursorWhile(runnable);
} catch (InterruptedException e) {
return;
} catch (InvocationTargetException e) {
throw e.getCause();
}
} catch (Throwable e) {
StringWriter message = new StringWriter();
PrintWriter out = new PrintWriter(message);
out.println(NLS.bind(Messages.ConvertMarkupToMarkup_cannot_generate_detail, targetMmarkupLanguage.getName(), e.getMessage()));
out.println(Messages.ConvertMarkupToMarkup_details_follow);
e.printStackTrace(out);
out.close();
MessageDialog.openError(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), NLS.bind(Messages.ConvertMarkupToMarkup_cannot_generate_title, targetMmarkupLanguage.getName()), message.toString());
}
}
Aggregations