use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.
the class WikiTextTest method getMarkupLanguageForFilename.
@Test
public void getMarkupLanguageForFilename() {
MarkupLanguage markupLanguage = WikiText.getMarkupLanguageForFilename("test.textile");
assertNotNull(markupLanguage);
assertEquals("Textile", markupLanguage.getName());
}
use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.
the class WikiTextContextStructureBridge method getOutline.
private OutlineItem getOutline(IFile file) {
// FIXME: is editor integration the way to go?? we probably need some kind of core model
IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (editorPart != null) {
OutlineItem outline = (OutlineItem) editorPart.getAdapter(OutlineItem.class);
if (outline != null) {
return outline;
}
}
MarkupLanguage markupLanguage = WikiText.getMarkupLanguageForFilename(file.getName());
if (markupLanguage != null) {
OutlineParser parser = new OutlineParser(markupLanguage);
try {
String contents = getContents(file);
OutlineItem outline = parser.parse(contents);
outline.setResourcePath(file.getFullPath().toString());
return outline;
} catch (Exception e) {
// ignore
return null;
}
}
return null;
}
use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage 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());
}
}
use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.
the class WikiToDocTask method markupToDoc.
private void markupToDoc(MarkupLanguage markupLanguage, Path path, String markupContent, Map<String, SplitOutlineItem> pathNameToOutline, Set<String> imageFilenames) throws BuildException {
File htmlOutputFile = computeHtmlOutputFile(path);
File pathDir = htmlOutputFile.getParentFile();
if (!pathDir.exists()) {
if (!pathDir.mkdirs()) {
throw new BuildException(// $NON-NLS-1$
MessageFormat.format(// $NON-NLS-1$
Messages.getString("WikiToDocTask_cannot_create_dest_folder"), pathDir.getAbsolutePath()));
}
}
Writer writer;
try {
// $NON-NLS-1$
writer = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(htmlOutputFile)), "utf-8");
} catch (Exception e) {
throw new BuildException(// $NON-NLS-1$
MessageFormat.format(// $NON-NLS-1$
Messages.getString("WikiToDocTask_cannot_create_output_file"), // $NON-NLS-1$
htmlOutputFile, e.getMessage()), e);
}
try {
HtmlDocumentBuilder builder = new HtmlDocumentBuilder(writer, formatOutput);
for (Stylesheet stylesheet : stylesheets) {
HtmlDocumentBuilder.Stylesheet builderStylesheet = createBuilderStylesheet(pathDir, stylesheet);
builder.addCssStylesheet(builderStylesheet);
}
builder.setTitle(computeTitle(path));
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);
MarkupLanguage markupLanguageClone = markupLanguage.clone();
if (markupLanguageClone instanceof MediaWikiLanguage) {
MediaWikiLanguage mediaWikiLanguage = (MediaWikiLanguage) markupLanguageClone;
mediaWikiLanguage.setPageMapping(new PathPageMapping(path, paths, pathNameToOutline));
if (imageFilenames != null) {
mediaWikiLanguage.setImageNames(imageFilenames);
}
}
SplitOutlineItem item = pathNameToOutline.get(path.name);
SplittingHtmlDocumentBuilder splittingBuilder = new SplittingHtmlDocumentBuilder();
splittingBuilder.setRootBuilder(builder);
splittingBuilder.setOutline(item);
splittingBuilder.setRootFile(htmlOutputFile);
splittingBuilder.setNavigationImages(navigationImages);
splittingBuilder.setFormatting(formatOutput);
splittingBuilder.setNavigationImagePath(computeNavigationImagePath(pathDir));
MarkupParser parser = new MarkupParser();
parser.setMarkupLanguage(markupLanguageClone);
parser.setBuilder(splittingBuilder);
parser.parse(markupContent);
} finally {
try {
writer.close();
} catch (Exception e) {
throw new BuildException(MessageFormat.format(// $NON-NLS-1$
Messages.getString("WikiToDocTask_cannot_write_output_file"), // $NON-NLS-1$
htmlOutputFile, e.getMessage()), e);
}
}
}
use of org.eclipse.mylyn.wikitext.parser.markup.MarkupLanguage in project mylyn.docs by eclipse.
the class WikiTextUiPlugin method getTemplates.
/**
* get templates mapped by their markup language name
*
* @return the templates
*/
public Map<String, Templates> getTemplates() {
if (templates == null) {
Map<String, Templates> templates = new HashMap<>();
IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(getPluginId(), EXTENSION_POINT_CONTENT_ASSIST);
if (extensionPoint != null) {
IConfigurationElement[] configurationElements = extensionPoint.getConfigurationElements();
for (IConfigurationElement element : configurationElements) {
String declaringPluginId = element.getDeclaringExtension().getContributor().getName();
if (EXTENSION_POINT_TEMPLATES.equals(element.getName())) {
try {
// $NON-NLS-1$
String markupLanguage = element.getAttribute("markupLanguage");
if (markupLanguage == null) {
throw new Exception(Messages.WikiTextUiPlugin_markupLanguageRequired);
} else if (!WikiText.getMarkupLanguageNames().contains(markupLanguage)) {
throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_invalidMarkupLanguage, new Object[] { markupLanguage }));
}
Templates markupLanguageTemplates = new Templates();
markupLanguageTemplates.setMarkupLanguageName(markupLanguage);
for (IConfigurationElement templatesChild : element.getChildren()) {
if (EXTENSION_POINT_TEMPLATE.equals(templatesChild.getName())) {
try {
// process the template
// $NON-NLS-1$
String name = templatesChild.getAttribute("name");
// $NON-NLS-1$
String description = templatesChild.getAttribute("description");
// $NON-NLS-1$
String content = templatesChild.getAttribute("content");
// $NON-NLS-1$
String autoInsert = templatesChild.getAttribute("autoInsert");
// $NON-NLS-1$
String block = templatesChild.getAttribute("block");
if (name == null || name.length() == 0) {
throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_nameRequired, new Object[] { EXTENSION_POINT_TEMPLATE }));
}
if (description == null || description.length() == 0) {
throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_descriptionRequired, new Object[] { EXTENSION_POINT_TEMPLATE }));
}
if (content == null || content.length() == 0) {
throw new Exception(NLS.bind(Messages.WikiTextUiPlugin_contentRequired, new Object[] { EXTENSION_POINT_TEMPLATE }));
}
// $NON-NLS-1$//$NON-NLS-2$
content = content.replace("\\t", "\t");
content = // $NON-NLS-1$
content.replace("\\r\\n", Text.DELIMITER).replace(// $NON-NLS-1$
"\\r", Text.DELIMITER).replace("\\n", // $NON-NLS-1$
Text.DELIMITER).replace("\\\\", // $NON-NLS-1$ //$NON-NLS-2$
"\\");
if (// $NON-NLS-1$
content.endsWith("$") && !(content.endsWith("\\$") || content.endsWith("$$"))) {
// $NON-NLS-1$ //$NON-NLS-2$
content = content.substring(0, content.length() - 1);
}
if (content.startsWith("^")) {
// $NON-NLS-1$
content = content.substring(1);
}
// $NON-NLS-1$ //$NON-NLS-2$
content = content.replace("\\$", "$$");
markupLanguageTemplates.addTemplate(new Template(name, description, MarkupTemplateCompletionProcessor.CONTEXT_ID, content, autoInsert == null || // $NON-NLS-1$
!"false".equalsIgnoreCase(autoInsert)), // $NON-NLS-1$
block != null && "true".equalsIgnoreCase(block));
} catch (Exception e) {
log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_invalidExtension, new Object[] { declaringPluginId, EXTENSION_POINT_CONTENT_ASSIST, e.getMessage() }), e);
}
} else {
log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_unexpectedExtensionElement, new Object[] { declaringPluginId, EXTENSION_POINT_CONTENT_ASSIST, templatesChild.getName() }), null);
}
}
Templates previous = templates.put(markupLanguageTemplates.getMarkupLanguageName(), markupLanguageTemplates);
if (previous != null) {
markupLanguageTemplates.addAll(previous);
}
} catch (Exception e) {
log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_invalidExtension, new Object[] { declaringPluginId, EXTENSION_POINT_TEMPLATES, e.getMessage() }), e);
}
} else {
log(IStatus.ERROR, NLS.bind(Messages.WikiTextUiPlugin_unexpectedExtensionElement, new Object[] { declaringPluginId, EXTENSION_POINT_CONTENT_ASSIST, element.getName() }), null);
}
}
}
// now that we have the basic templates, check for language extensions and connect the hierarchy
// first ensure that all language names have templates defined
Set<String> languageNames = WikiText.getMarkupLanguageNames();
for (String languageName : languageNames) {
Templates languageTemplates = templates.get(languageName);
if (languageTemplates == null) {
languageTemplates = new Templates();
templates.put(languageName, languageTemplates);
}
}
// next connect the hierarchy
for (String languageName : languageNames) {
MarkupLanguage markupLanguage = WikiText.getMarkupLanguage(languageName);
if (markupLanguage != null && markupLanguage.getExtendsLanguage() != null) {
Templates languageTemplates = templates.get(languageName);
Templates parentLanguageTemplates = templates.get(markupLanguage.getExtendsLanguage());
languageTemplates.setParent(parentLanguageTemplates);
}
}
this.templates = Collections.unmodifiableMap(templates);
}
return templates;
}
Aggregations