Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 186342 Details for
Bug 319977
wikitext-to-eclipse-help for Trac doesn't support images
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
Patch to add image macro support to Trac wiki parser
0001-Add-image-macro-support-to-Trac-wiki-parser.patch (text/plain), 12.71 KB, created by
Mat Booth
on 2011-01-09 12:38:26 EST
(
hide
)
Description:
Patch to add image macro support to Trac wiki parser
Filename:
MIME Type:
Creator:
Mat Booth
Created:
2011-01-09 12:38:26 EST
Size:
12.71 KB
patch
obsolete
>From f5d1ec6a8f75d996eafd4bb3446f51e461a1c7da Mon Sep 17 00:00:00 2001 >From: mbooth <fedora@matbooth.co.uk> >Date: Sun, 9 Jan 2011 17:27:51 +0000 >Subject: [PATCH 1/2] Add image macro support to Trac wiki parser. > >--- > .../wikitext/core/parser/ImageAttributes.java | 50 +++++++ > .../core/parser/builder/HtmlDocumentBuilder.java | 14 ++- > .../tracwiki/core/token/MacroReplacementToken.java | 137 ++++++++++++++++++++ > .../wikitext/tracwiki/core/TracWikiLanguage.java | 4 +- > 4 files changed, 201 insertions(+), 4 deletions(-) > create mode 100644 org.eclipse.mylyn.wikitext.tracwiki.core/src/org/eclipse/mylyn/internal/wikitext/tracwiki/core/token/MacroReplacementToken.java > >diff --git a/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/wikitext/core/parser/ImageAttributes.java b/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/wikitext/core/parser/ImageAttributes.java >index ce59426..09f250d 100644 >--- a/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/wikitext/core/parser/ImageAttributes.java >+++ b/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/wikitext/core/parser/ImageAttributes.java >@@ -24,8 +24,12 @@ public class ImageAttributes extends Attributes { > > private int width = -1; > >+ private boolean widthPercentage = false; >+ > private int height = -1; > >+ private boolean heightPercentage = false; >+ > private int border = 0; > > private Align align = null; >@@ -71,4 +75,50 @@ public class ImageAttributes extends Attributes { > public void setAlign(Align align) { > this.align = align; > } >+ >+ /** >+ * Sets whether the value passed into {@link #setWidth(int)} should be treated as a percentage or as a number of >+ * pixels. The default is false (pixels) to maintain backwards compatibility. >+ * >+ * @param widthPercentage >+ * pass true if the width is specified as a percentage, false if the width is specified in pixels >+ * @since 1.4 >+ */ >+ public void setWidthPercentage(boolean widthPercentage) { >+ this.widthPercentage = widthPercentage; >+ } >+ >+ /** >+ * Tells you whether the value returned from {@link #getWidth()} is the width as a percentage or in pixels. >+ * >+ * @return true if the width should be treated as a percentage, false if the width should be treated as an amount of >+ * pixels >+ * @since 1.4 >+ */ >+ public boolean isWidthPercentage() { >+ return widthPercentage; >+ } >+ >+ /** >+ * Sets whether the value passed into {@link #setHeight(int)} should be treated as a percentage or as a number of >+ * pixels. The default is false (pixels) to maintain backwards compatibility. >+ * >+ * @param heightPercentage >+ * pass true if the height is specified as a percentage, false if the height is specified in pixels >+ * @since 1.4 >+ */ >+ public void setHeightPercentage(boolean heightPercentage) { >+ this.heightPercentage = heightPercentage; >+ } >+ >+ /** >+ * Tells you whether the value returned from {@link #getHeight()} is the height as a percentage or in pixels. >+ * >+ * @return true if the height should be treated as a percentage, false if the height should be treated as an amount >+ * of pixels >+ * @since 1.4 >+ */ >+ public boolean isHeightPercentage() { >+ return heightPercentage; >+ } > } >diff --git a/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/wikitext/core/parser/builder/HtmlDocumentBuilder.java b/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/wikitext/core/parser/builder/HtmlDocumentBuilder.java >index 6ce531f..b942683 100644 >--- a/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/wikitext/core/parser/builder/HtmlDocumentBuilder.java >+++ b/org.eclipse.mylyn.wikitext.core/src/org/eclipse/mylyn/wikitext/core/parser/builder/HtmlDocumentBuilder.java >@@ -27,13 +27,13 @@ import java.util.regex.Pattern; > > import org.eclipse.mylyn.wikitext.core.parser.Attributes; > import org.eclipse.mylyn.wikitext.core.parser.ImageAttributes; >+import org.eclipse.mylyn.wikitext.core.parser.ImageAttributes.Align; > import org.eclipse.mylyn.wikitext.core.parser.LinkAttributes; > import org.eclipse.mylyn.wikitext.core.parser.ListAttributes; > import org.eclipse.mylyn.wikitext.core.parser.QuoteAttributes; > import org.eclipse.mylyn.wikitext.core.parser.TableAttributes; > import org.eclipse.mylyn.wikitext.core.parser.TableCellAttributes; > import org.eclipse.mylyn.wikitext.core.parser.TableRowAttributes; >-import org.eclipse.mylyn.wikitext.core.parser.ImageAttributes.Align; > import org.eclipse.mylyn.wikitext.core.util.DefaultXmlStreamWriter; > import org.eclipse.mylyn.wikitext.core.util.FormattingXMLStreamWriter; > import org.eclipse.mylyn.wikitext.core.util.XmlStreamWriter; >@@ -893,10 +893,18 @@ public class HtmlDocumentBuilder extends AbstractXmlDocumentBuilder { > if (attributes instanceof ImageAttributes) { > ImageAttributes imageAttributes = (ImageAttributes) attributes; > if (imageAttributes.getHeight() != -1) { >- writer.writeAttribute("height", Integer.toString(imageAttributes.getHeight())); //$NON-NLS-1$ >+ String val = Integer.toString(imageAttributes.getHeight()); >+ if (imageAttributes.isHeightPercentage()) { >+ val += "%"; //$NON-NLS-1$ >+ } >+ writer.writeAttribute("height", val); //$NON-NLS-1$ > } > if (imageAttributes.getWidth() != -1) { >- writer.writeAttribute("width", Integer.toString(imageAttributes.getWidth())); //$NON-NLS-1$ >+ String val = Integer.toString(imageAttributes.getWidth()); >+ if (imageAttributes.isWidthPercentage()) { >+ val += "%"; //$NON-NLS-1$ >+ } >+ writer.writeAttribute("width", val); //$NON-NLS-1$ > } > if (!xhtmlStrict && align != null) { > writer.writeAttribute("align", align.name().toLowerCase()); //$NON-NLS-1$ >diff --git a/org.eclipse.mylyn.wikitext.tracwiki.core/src/org/eclipse/mylyn/internal/wikitext/tracwiki/core/token/MacroReplacementToken.java b/org.eclipse.mylyn.wikitext.tracwiki.core/src/org/eclipse/mylyn/internal/wikitext/tracwiki/core/token/MacroReplacementToken.java >new file mode 100644 >index 0000000..3f951f6 >--- /dev/null >+++ b/org.eclipse.mylyn.wikitext.tracwiki.core/src/org/eclipse/mylyn/internal/wikitext/tracwiki/core/token/MacroReplacementToken.java >@@ -0,0 +1,137 @@ >+/******************************************************************************* >+ * Copyright (c) 2010 Mat Booth and others. >+ * All rights reserved. This program and the accompanying materials >+ * are made available under the terms of the Eclipse Public License v1.0 >+ * which accompanies this distribution, and is available at >+ * http://www.eclipse.org/legal/epl-v10.html >+ * >+ * Contributors: >+ * Mat Booth >+ *******************************************************************************/ >+package org.eclipse.mylyn.internal.wikitext.tracwiki.core.token; >+ >+import java.util.regex.Matcher; >+import java.util.regex.Pattern; >+ >+import org.eclipse.mylyn.wikitext.core.parser.ImageAttributes; >+import org.eclipse.mylyn.wikitext.core.parser.markup.PatternBasedElement; >+import org.eclipse.mylyn.wikitext.core.parser.markup.PatternBasedElementProcessor; >+ >+/** >+ * Detects macro tags of the format: <code>[[MacroName]]</code> or <code>[[MacroName(arg1,arg2,...)]]</code> >+ * >+ * @author Mat Booth >+ */ >+public class MacroReplacementToken extends PatternBasedElement { >+ >+ @Override >+ protected String getPattern(int groupOffset) { >+ return "(?:\\[\\[([a-zA-Z]+)(?:\\(((?:(?!\\]\\]).)*)\\))?\\]\\])"; //$NON-NLS-1$ >+ } >+ >+ @Override >+ protected int getPatternGroupCount() { >+ return 2; >+ } >+ >+ @Override >+ protected PatternBasedElementProcessor newProcessor() { >+ return new MacroReplacementTokenProcessor(); >+ } >+ >+ private static class MacroReplacementTokenProcessor extends PatternBasedElementProcessor { >+ @Override >+ public void emit() { >+ String macro = group(1); >+ String[] args; >+ if (group(2) != null && group(2).length() > 0) { >+ args = group(2).split(","); //$NON-NLS-1$ >+ } else { >+ args = new String[0]; >+ } >+ >+ // Handle image macro >+ if (macro.equalsIgnoreCase("Image")) { //$NON-NLS-1$ >+ imageMacro(args); >+ } >+ >+ // TODO: Support more macros! All others are currently ignored. >+ } >+ >+ /** >+ * Process the image macro. The first argument is a mandatory file specification. Remaining arguments specify >+ * optional attributes and styles. >+ * >+ * @param args >+ * list of arguments passed into the macro >+ */ >+ public void imageMacro(String[] args) { >+ // Don't do anything if we don't get at least one argument >+ if (args.length < 1) { >+ return; >+ } >+ >+ // Border thickness attribute pattern >+ Pattern borderPattern = Pattern.compile("border=([0-9]+)"); //$NON-NLS-1$ >+ >+ // Size attribute pattern >+ Pattern sizePattern = Pattern.compile("(?:(width|height)=|^)?([0-9]+)(%|px)?"); //$NON-NLS-1$ >+ >+ // Floating alignment attribute pattern >+ Pattern alignPattern = Pattern.compile("(?:align=)?(right|left|top|bottom)"); //$NON-NLS-1$ >+ >+ ImageAttributes attributes = new ImageAttributes(); >+ for (int i = 1; i < args.length; i++) { >+ String arg = args[i].trim(); >+ Matcher m; >+ >+ // Border thickness attribute >+ m = borderPattern.matcher(arg); >+ if (m.matches()) { >+ attributes.setBorder(Integer.parseInt(m.group(1))); >+ continue; >+ } >+ // Size attribute >+ m = sizePattern.matcher(arg); >+ if (m.matches()) { >+ int size = Integer.parseInt(m.group(2)); >+ if (m.group(1) == null || m.group(1).length() == 0 || m.group(1).equalsIgnoreCase("width")) { //$NON-NLS-1$ >+ attributes.setWidth(size); >+ attributes.setWidthPercentage(m.group(3) != null && m.group(3).equals("%")); //$NON-NLS-1$ >+ } else { >+ attributes.setHeight(size); >+ attributes.setHeightPercentage(m.group(3) != null && m.group(3).equals("%")); //$NON-NLS-1$ >+ } >+ continue; >+ } >+ // Floating alignment attribute >+ // (to create nice layouts, Trac translates the "align" parameter to a float CSS style >+ // instead of text-align or vertical-align, so we should emulate that here) >+ m = alignPattern.matcher(arg); >+ if (m.matches()) { >+ attributes.appendCssStyle("float:" + m.group(1) + ";"); //$NON-NLS-1$ //$NON-NLS-2$ >+ continue; >+ } >+ >+ // All other key/value pair attributes that aren't syntactically special >+ String[] parts = arg.split("="); //$NON-NLS-1$ >+ if (parts.length == 2) { >+ if (parts[0].equalsIgnoreCase("id")) { //$NON-NLS-1$ >+ attributes.setId(parts[1]); >+ } >+ if (parts[0].equalsIgnoreCase("class")) { //$NON-NLS-1$ >+ attributes.appendCssClass(parts[1]); >+ } >+ if (parts[0].equalsIgnoreCase("alt")) { //$NON-NLS-1$ >+ attributes.setAlt(parts[1]); >+ } >+ if (parts[0].equalsIgnoreCase("title")) { //$NON-NLS-1$ >+ attributes.setTitle(parts[1]); >+ } >+ } >+ } >+ >+ builder.image(attributes, args[0]); >+ } >+ } >+} >diff --git a/org.eclipse.mylyn.wikitext.tracwiki.core/src/org/eclipse/mylyn/wikitext/tracwiki/core/TracWikiLanguage.java b/org.eclipse.mylyn.wikitext.tracwiki.core/src/org/eclipse/mylyn/wikitext/tracwiki/core/TracWikiLanguage.java >index d439f8c..20ee5c8 100644 >--- a/org.eclipse.mylyn.wikitext.tracwiki.core/src/org/eclipse/mylyn/wikitext/tracwiki/core/TracWikiLanguage.java >+++ b/org.eclipse.mylyn.wikitext.tracwiki.core/src/org/eclipse/mylyn/wikitext/tracwiki/core/TracWikiLanguage.java >@@ -27,6 +27,7 @@ import org.eclipse.mylyn.internal.wikitext.tracwiki.core.token.BangEscapeToken; > import org.eclipse.mylyn.internal.wikitext.tracwiki.core.token.ChangesetLinkReplacementToken; > import org.eclipse.mylyn.internal.wikitext.tracwiki.core.token.HyperlinkReplacementToken; > import org.eclipse.mylyn.internal.wikitext.tracwiki.core.token.LineBreakToken; >+import org.eclipse.mylyn.internal.wikitext.tracwiki.core.token.MacroReplacementToken; > import org.eclipse.mylyn.internal.wikitext.tracwiki.core.token.MilestoneLinkReplacementToken; > import org.eclipse.mylyn.internal.wikitext.tracwiki.core.token.ReportLinkReplacementToken; > import org.eclipse.mylyn.internal.wikitext.tracwiki.core.token.RevisionLogReplacementToken; >@@ -236,7 +237,7 @@ public class TracWikiLanguage extends AbstractMarkupLanguage { > protected void addStandardBlocks(List<Block> blocks, List<Block> paragraphBreakingBlocks) { > // IMPORTANT NOTE: Most items below have order dependencies. DO NOT REORDER ITEMS BELOW!! > >- // TODO: images, macros, processors >+ // TODO: processors > > ListBlock listBlock = new ListBlock(); > blocks.add(listBlock); >@@ -279,6 +280,7 @@ public class TracWikiLanguage extends AbstractMarkupLanguage { > tokenSyntax.add(new BangEscapeToken()); > tokenSyntax.add(new LineBreakToken()); > tokenSyntax.beginGroup("(?:(?<=[\\s\\.\\\"'?!;:\\)\\(\\{\\}\\[\\]-])|^)(?:", 0); // always starts at the start of a line or after a non-word character excluding '!' and '-' //$NON-NLS-1$ >+ tokenSyntax.add(new MacroReplacementToken()); > tokenSyntax.add(new RevisionLogReplacementToken()); > tokenSyntax.add(new ChangesetLinkReplacementToken()); > tokenSyntax.add(new HyperlinkReplacementToken()); >-- >1.7.3.4 >
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Flags:
greensopinion
:
iplog+
Actions:
View
|
Diff
Attachments on
bug 319977
:
185605
|
186243
| 186342 |
186343
|
186403