Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 35.14 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Index: src/test/java/org/apache/servicemix/docs/confluence/ConfluenceDocumentTest.java
  2. ===================================================================
  3. --- src/test/java/org/apache/servicemix/docs/confluence/ConfluenceDocumentTest.java     (revision 954753)
  4. +++ src/test/java/org/apache/servicemix/docs/confluence/ConfluenceDocumentTest.java     (working copy)
  5. @@ -%ld,%ld +%ld,%ld @@
  6.          writer.flush();
  7.          writer.close();
  8.  
  9. +        System.err.println(writer.toString());
  10. +
  11.          String[] results = writer.toString().split("\\r?\\n\\r?");
  12.          int i = 0;
  13.  
  14. Index: src/test/java/org/apache/servicemix/docs/confluence/WikitextTest.java
  15. ===================================================================
  16. --- src/test/java/org/apache/servicemix/docs/confluence/WikitextTest.java       (revision 0)
  17. +++ src/test/java/org/apache/servicemix/docs/confluence/WikitextTest.java       (revision 0)
  18. @@ -%ld,%ld +%ld,%ld @@
  19. +package org.apache.servicemix.docs.confluence;
  20. +
  21. +import java.io.FileInputStream;
  22. +import java.io.InputStream;
  23. +import java.io.InputStreamReader;
  24. +import java.io.StringWriter;
  25. +
  26. +import org.apache.commons.io.IOUtils;
  27. +import org.eclipse.mylyn.wikitext.confluence.core.ConfluenceLanguage;
  28. +import org.eclipse.mylyn.wikitext.core.parser.MarkupParser;
  29. +import org.eclipse.mylyn.wikitext.core.parser.builder.DocBookDocumentBuilder;
  30. +import org.eclipse.mylyn.wikitext.core.parser.markup.MarkupLanguage;
  31. +
  32. +/**
  33. + * Created by IntelliJ IDEA.
  34. + * User: gnodet
  35. + * Date: Jun 15, 2010
  36. + * Time: 5:33:51 PM
  37. + * To change this template use File | Settings | File Templates.
  38. + */
  39. +public class WikitextTest {
  40. +
  41. +    @org.junit.Test
  42. +    public void testWikitext() throws Exception {
  43. +//        InputStream is = getClass().getResource("lists.wiki").openStream();
  44. +        InputStream is = new FileInputStream("/Users/gnodet/work/felix/git/karaf/manual/src/confluence/installation.wiki");
  45. +        String markupContent = IOUtils.toString(is);
  46. +        MarkupLanguage markupLanguage = new ConfluenceLanguage();
  47. +        StringWriter writer = new StringWriter();
  48. +        DocBookDocumentBuilder builder = new DocBookDocumentBuilder(writer);
  49. +        MarkupParser parser = new MarkupParser();
  50. +        parser.setMarkupLanguage(markupLanguage);
  51. +        parser.setBuilder(builder);
  52. +        parser.parse(markupContent);
  53. +        writer.flush();
  54. +        String out = writer.toString();
  55. +        int beg = out.indexOf("<chapter");
  56. +        int end = out.indexOf("</book>");
  57. +        out = out.substring(beg, end);
  58. +        System.err.println(out);
  59. +
  60. +        writer = new StringWriter();
  61. +        ConfluenceConverter document =
  62. +                new ConfluenceConverter(new InputStreamReader(new FileInputStream("/Users/gnodet/work/felix/git/karaf/manual/src/confluence/installation.wiki")),
  63. +                                        writer);
  64. +
  65. +        document.convert();
  66. +        writer.flush();
  67. +        writer.close();
  68. +        out = writer.toString();
  69. +        System.err.println(out);
  70. +    }
  71. +}
  72. Index: src/main/java/org/apache/servicemix/docs/confluence/CodeBlock.java
  73. ===================================================================
  74. --- src/main/java/org/apache/servicemix/docs/confluence/CodeBlock.java  (revision 0)
  75. +++ src/main/java/org/apache/servicemix/docs/confluence/CodeBlock.java  (revision 0)
  76. @@ -%ld,%ld +%ld,%ld @@
  77. +package org.apache.servicemix.docs.confluence;
  78. +
  79. +import org.eclipse.mylyn.internal.wikitext.confluence.core.block.AbstractConfluenceDelimitedBlock;
  80. +import org.eclipse.mylyn.wikitext.core.parser.Attributes;
  81. +import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder;
  82. +
  83. +/**
  84. + * Created by IntelliJ IDEA.
  85. + * User: gnodet
  86. + * Date: Jun 15, 2010
  87. + * Time: 9:44:44 PM
  88. + * To change this template use File | Settings | File Templates.
  89. + */
  90. +public class CodeBlock extends AbstractConfluenceDelimitedBlock {
  91. +
  92. +       private String title;
  93. +
  94. +       private String language;
  95. +
  96. +       public CodeBlock() {
  97. +               super("code"); //$NON-NLS-1$
  98. +       }
  99. +
  100. +       @Override
  101. +       protected void beginBlock() {
  102. +               if (title != null) {
  103. +                       Attributes attributes = new Attributes();
  104. +                       attributes.setTitle(title);
  105. +                       builder.beginBlock(DocumentBuilder.BlockType.PANEL, attributes);
  106. +               }
  107. +               Attributes attributes = new Attributes();
  108. +               Attributes preAttributes = new Attributes();
  109. +               if (language != null) {
  110. +                       attributes.setLanguage(language); //$NON-NLS-1$
  111. +               }
  112. +//             builder.beginBlock(DocumentBuilder.BlockType.PREFORMATTED, preAttributes);
  113. +               builder.beginBlock(DocumentBuilder.BlockType.CODE, attributes);
  114. +        builder.charactersUnescaped("<![CDATA[");
  115. +       }
  116. +
  117. +       @Override
  118. +       protected void handleBlockContent(String content) {
  119. +               builder.charactersUnescaped(content);
  120. +               builder.charactersUnescaped("\n"); //$NON-NLS-1$
  121. +       }
  122. +
  123. +       @Override
  124. +       protected void endBlock() {
  125. +        builder.charactersUnescaped("]]>");
  126. +               if (title != null) {
  127. +                       builder.endBlock(); // panel
  128. +               }
  129. +               builder.endBlock(); // code
  130. +//             builder.endBlock(); // pre
  131. +       }
  132. +
  133. +       @Override
  134. +       protected void resetState() {
  135. +               super.resetState();
  136. +               title = null;
  137. +       }
  138. +
  139. +       @Override
  140. +       protected void setOption(String key, String value) {
  141. +               if (key.equals("title")) { //$NON-NLS-1$
  142. +                       title = value;
  143. +               } else if (key.equals("lang")) {
  144. +            language = value;
  145. +        }
  146. +       }
  147. +
  148. +       @Override
  149. +       protected void setOption(String option) {
  150. +               language = option.toLowerCase();
  151. +       }
  152. +}
  153. Index: src/main/java/org/apache/servicemix/docs/confluence/ConfluenceLanguage.java
  154. ===================================================================
  155. --- src/main/java/org/apache/servicemix/docs/confluence/ConfluenceLanguage.java (revision 0)
  156. +++ src/main/java/org/apache/servicemix/docs/confluence/ConfluenceLanguage.java (revision 0)
  157. @@ -%ld,%ld +%ld,%ld @@
  158. +package org.apache.servicemix.docs.confluence;
  159. +
  160. +import java.util.ArrayList;
  161. +import java.util.List;
  162. +
  163. +import org.eclipse.mylyn.internal.wikitext.confluence.core.block.ColorBlock;
  164. +import org.eclipse.mylyn.internal.wikitext.confluence.core.block.ExtendedPreformattedBlock;
  165. +import org.eclipse.mylyn.internal.wikitext.confluence.core.block.ExtendedQuoteBlock;
  166. +import org.eclipse.mylyn.internal.wikitext.confluence.core.block.HeadingBlock;
  167. +import org.eclipse.mylyn.internal.wikitext.confluence.core.block.ListBlock;
  168. +import org.eclipse.mylyn.internal.wikitext.confluence.core.block.QuoteBlock;
  169. +import org.eclipse.mylyn.internal.wikitext.confluence.core.block.TableBlock;
  170. +import org.eclipse.mylyn.internal.wikitext.confluence.core.block.TableOfContentsBlock;
  171. +import org.eclipse.mylyn.internal.wikitext.confluence.core.block.TextBoxBlock;
  172. +import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder.BlockType;
  173. +import org.eclipse.mylyn.wikitext.core.parser.markup.Block;
  174. +
  175. +public class ConfluenceLanguage extends org.eclipse.mylyn.wikitext.confluence.core.ConfluenceLanguage {
  176. +
  177. +    private final List<Block> nestedBlocks = new ArrayList<Block>();
  178. +
  179. +    public ConfluenceLanguage() {
  180. +    }
  181. +
  182. +    @Override
  183. +    protected void clearLanguageSyntax() {
  184. +        super.clearLanguageSyntax();
  185. +        nestedBlocks.clear();
  186. +    }
  187. +
  188. +    public List<Block> getNestedBlocks() {
  189. +        return nestedBlocks;
  190. +    }
  191. +
  192. +    @Override
  193. +    protected void addStandardBlocks(List<Block> blocks, List<Block> paragraphBreakingBlocks) {
  194. +        // IMPORTANT NOTE: Most items below have order dependencies.  DO NOT REORDER ITEMS BELOW!!
  195. +
  196. +        HeadingBlock headingBlock = new HeadingBlock();
  197. +        blocks.add(headingBlock);
  198. +        paragraphBreakingBlocks.add(headingBlock);
  199. +        nestedBlocks.add(headingBlock);
  200. +        ListBlock listBlock = new ListBlock();
  201. +        blocks.add(listBlock);
  202. +        paragraphBreakingBlocks.add(listBlock);
  203. +        nestedBlocks.add(listBlock);
  204. +        blocks.add(new QuoteBlock());
  205. +        TableBlock tableBlock = new TableBlock();
  206. +        blocks.add(tableBlock);
  207. +        paragraphBreakingBlocks.add(tableBlock);
  208. +        nestedBlocks.add(tableBlock);
  209. +        ExtendedQuoteBlock quoteBlock = new ExtendedQuoteBlock();
  210. +        blocks.add(quoteBlock);
  211. +        paragraphBreakingBlocks.add(quoteBlock);
  212. +        ExtendedPreformattedBlock noformatBlock = new ExtendedPreformattedBlock();
  213. +        blocks.add(noformatBlock);
  214. +        paragraphBreakingBlocks.add(noformatBlock);
  215. +
  216. +        blocks.add(new TextBoxBlock(BlockType.PANEL, "panel")); //$NON-NLS-1$
  217. +        blocks.add(new TextBoxBlock(BlockType.NOTE, "note")); //$NON-NLS-1$
  218. +        blocks.add(new TextBoxBlock(BlockType.INFORMATION, "info")); //$NON-NLS-1$
  219. +        blocks.add(new TextBoxBlock(BlockType.WARNING, "warning")); //$NON-NLS-1$
  220. +        blocks.add(new TextBoxBlock(BlockType.TIP, "tip")); //$NON-NLS-1$
  221. +        CodeBlock codeBlock = new CodeBlock();
  222. +        blocks.add(codeBlock);
  223. +        paragraphBreakingBlocks.add(codeBlock);
  224. +        blocks.add(new TableOfContentsBlock());
  225. +        ColorBlock colorBlock = new ColorBlock();
  226. +        blocks.add(colorBlock);
  227. +        paragraphBreakingBlocks.add(colorBlock);
  228. +    }
  229. +
  230. +}
  231. Index: src/main/java/org/apache/servicemix/docs/confluence/DocBookDocumentBuilder.java
  232. ===================================================================
  233. --- src/main/java/org/apache/servicemix/docs/confluence/DocBookDocumentBuilder.java     (revision 0)
  234. +++ src/main/java/org/apache/servicemix/docs/confluence/DocBookDocumentBuilder.java     (revision 0)
  235. @@ -%ld,%ld +%ld,%ld @@
  236. +package org.apache.servicemix.docs.confluence;
  237. +
  238. +import java.io.Writer;
  239. +import java.util.HashMap;
  240. +import java.util.HashSet;
  241. +import java.util.Iterator;
  242. +import java.util.Map;
  243. +import java.util.Set;
  244. +import java.util.Stack;
  245. +import java.util.TreeMap;
  246. +import java.util.logging.Logger;
  247. +import java.util.regex.Matcher;
  248. +import java.util.regex.Pattern;
  249. +
  250. +import org.eclipse.mylyn.internal.wikitext.core.util.css.CssParser;
  251. +import org.eclipse.mylyn.internal.wikitext.core.util.css.CssRule;
  252. +import org.eclipse.mylyn.wikitext.core.parser.Attributes;
  253. +import org.eclipse.mylyn.wikitext.core.parser.DocumentBuilder;
  254. +import org.eclipse.mylyn.wikitext.core.parser.LinkAttributes;
  255. +import org.eclipse.mylyn.wikitext.core.parser.builder.AbstractXmlDocumentBuilder;
  256. +import org.eclipse.mylyn.wikitext.core.parser.util.MarkupToDocbook;
  257. +import org.eclipse.mylyn.wikitext.core.util.FormattingXMLStreamWriter;
  258. +import org.eclipse.mylyn.wikitext.core.util.XmlStreamWriter;
  259. +import org.eclipse.mylyn.wikitext.core.util.anttask.MarkupToDocbookTask;
  260. +
  261. +/**
  262. + * A builder that can emit <a href="http://www.docbook.org/">Docbook</a>
  263. + *
  264. + * @author David Green
  265. + * @author Peter Friese bug 273355 Support image scaling for Textile -> DocBook
  266. + * @see MarkupToDocbook
  267. + * @see MarkupToDocbookTask
  268. + * @see DitaBookMapDocumentBuilder
  269. + * @since 1.0
  270. + */
  271. +public class DocBookDocumentBuilder extends AbstractXmlDocumentBuilder {
  272. +
  273. +       private static final Pattern PERCENTAGE = Pattern.compile("(\\d+)%"); //$NON-NLS-1$
  274. +
  275. +       private static final Pattern CSS_CLASS_INLINE = Pattern.compile("(^|\\s+)inline(\\s+|$)"); //$NON-NLS-1$
  276. +
  277. +       private static Set<Integer> entityReferenceToUnicode = new HashSet<Integer>();
  278. +       static {
  279. +               entityReferenceToUnicode.add(215);
  280. +               entityReferenceToUnicode.add(8211);
  281. +               entityReferenceToUnicode.add(8212);
  282. +               entityReferenceToUnicode.add(8220);
  283. +               entityReferenceToUnicode.add(8221);
  284. +               entityReferenceToUnicode.add(8216);
  285. +               entityReferenceToUnicode.add(8217);
  286. +
  287. +       }
  288. +
  289. +//     private String bookTitle;
  290. +
  291. +       private String doctype = "<!DOCTYPE book PUBLIC \"-//OASIS//DTD DocBook XML V4.5//EN\" \"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd\">"; //$NON-NLS-1$
  292. +
  293. +       private final Map<String, String> acronyms = new HashMap<String, String>();
  294. +
  295. +       private int headingLevel = 0;
  296. +
  297. +       private final Stack<BlockDescription> blockDescriptions = new Stack<BlockDescription>();
  298. +
  299. +       private boolean automaticGlossary = true;
  300. +
  301. +       public DocBookDocumentBuilder(Writer out) {
  302. +               super(out);
  303. +       }
  304. +
  305. +       public DocBookDocumentBuilder(XmlStreamWriter writer) {
  306. +               super(writer);
  307. +       }
  308. +
  309. +       protected XmlStreamWriter createFormattingXmlStreamWriter(Writer out) {
  310. +               XmlStreamWriter writer = super.createXmlStreamWriter(out);
  311. +               return new FormattingXMLStreamWriter(writer) {
  312. +                       @Override
  313. +                       protected boolean preserveWhitespace(String elementName) {
  314. +                               return elementName.equals("programlisting") || elementName.equals("code") || elementName.startsWith("literal"); //$NON-NLS-1$ //$NON-NLS-2$
  315. +                       }
  316. +               };
  317. +       }
  318. +
  319. +       public String getDoctype() {
  320. +               return doctype;
  321. +       }
  322. +
  323. +       public void setDoctype(String doctype) {
  324. +               this.doctype = doctype;
  325. +       }
  326. +
  327. +//     public String getBookTitle() {
  328. +//             return bookTitle;
  329. +//     }
  330. +//
  331. +//     public void setBookTitle(String bookTitle) {
  332. +//             this.bookTitle = bookTitle;
  333. +//     }
  334. +
  335. +       @Override
  336. +       public void acronym(String text, String definition) {
  337. +               String previousDef = acronyms.put(text, definition);
  338. +               if (previousDef != null && previousDef.length() > definition.length()) {
  339. +                       acronyms.put(text, previousDef);
  340. +               }
  341. +               writer.writeStartElement("glossterm"); //$NON-NLS-1$
  342. +               characters(text);
  343. +               writer.writeEndElement();
  344. +       }
  345. +
  346. +       @Override
  347. +       public void link(Attributes attributes, String href, final String text) {
  348. +               link(attributes, href, new ContentEmitter() {
  349. +                       public void emit() {
  350. +                               writer.writeCharacters(text);
  351. +                       }
  352. +               });
  353. +       }
  354. +
  355. +       private void link(Attributes attributes, String href, ContentEmitter emitter) {
  356. +               ensureBlockElementsOpen();
  357. +               if (href.startsWith("#")) { //$NON-NLS-1$
  358. +                       if (href.length() > 1) {
  359. +                               writer.writeStartElement("link"); //$NON-NLS-1$
  360. +                               writer.writeAttribute("linkend", href.substring(1)); //$NON-NLS-1$
  361. +                               emitter.emit();
  362. +                               writer.writeEndElement(); // link
  363. +                       } else {
  364. +                               emitter.emit();
  365. +                       }
  366. +               } else {
  367. +                       writer.writeStartElement("ulink"); //$NON-NLS-1$
  368. +                       writer.writeAttribute("url", href); //$NON-NLS-1$
  369. +                       emitter.emit();
  370. +                       writer.writeEndElement(); // ulink
  371. +               }
  372. +       }
  373. +
  374. +       private interface ContentEmitter {
  375. +               public void emit();
  376. +       }
  377. +
  378. +       @Override
  379. +       public void beginBlock(BlockType type, Attributes attributes) {
  380. +               if (headingLevel == 0) {
  381. +                       beginHeading(1, new Attributes());
  382. +                       endHeading();
  383. +               }
  384. +
  385. +               String elementName;
  386. +               String[] elementNames = null;
  387. +               boolean allowTitle = false;
  388. +               boolean closeElementsOnBlockStart = false;
  389. +               BlockDescription previousBlock = null;
  390. +               if (!blockDescriptions.isEmpty()) {
  391. +                       previousBlock = blockDescriptions.peek();
  392. +               }
  393. +
  394. +               switch (type) {
  395. +               case BULLETED_LIST:
  396. +                       elementName = "itemizedlist"; //$NON-NLS-1$
  397. +                       break;
  398. +               case NUMERIC_LIST:
  399. +                       elementName = "orderedlist"; //$NON-NLS-1$
  400. +                       break;
  401. +               case DEFINITION_LIST:
  402. +                       elementName = "variablelist"; //$NON-NLS-1$
  403. +
  404. +                       //                      variablelist
  405. +                       //                              varlistentry+
  406. +                       //                                      term+
  407. +                       //                                      listitem
  408. +                       //
  409. +                       break;
  410. +               case DEFINITION_TERM:
  411. +
  412. +                       BlockDescription blockDescription = findBlockDescription(BlockType.DEFINITION_LIST);
  413. +                       if (blockDescription.entrySize > 0) {
  414. +                               endBlockEntry(blockDescription);
  415. +                       }
  416. +                       openBlockEntry(blockDescription, new String[] { "varlistentry" }); //$NON-NLS-1$
  417. +
  418. +                       elementName = "term"; //$NON-NLS-1$
  419. +                       break;
  420. +               case DEFINITION_ITEM:
  421. +                       elementName = "listitem"; //$NON-NLS-1$
  422. +                       elementNames = new String[] { "para" }; //$NON-NLS-1$
  423. +                       closeElementsOnBlockStart = true;
  424. +                       break;
  425. +               case FOOTNOTE:
  426. +               case PARAGRAPH:
  427. +                       elementName = "para"; //$NON-NLS-1$
  428. +                       break;
  429. +               case CODE:
  430. +                       elementName = "programlisting"; //$NON-NLS-1$
  431. +                       break;
  432. +               case PREFORMATTED:
  433. +                       elementName = "literallayout"; //$NON-NLS-1$
  434. +                       break;
  435. +               case QUOTE:
  436. +                       elementName = "blockquote"; //$NON-NLS-1$
  437. +                       break;
  438. +               case LIST_ITEM:
  439. +                       elementName = "listitem"; //$NON-NLS-1$
  440. +                       elementNames = new String[] { "para" }; //$NON-NLS-1$
  441. +                       closeElementsOnBlockStart = true;
  442. +                       break;
  443. +               case TABLE:
  444. +                       elementName = "informaltable"; //$NON-NLS-1$
  445. +                       break;
  446. +               case TABLE_CELL_HEADER:
  447. +                       elementName = "th"; //$NON-NLS-1$
  448. +                       break;
  449. +               case TABLE_CELL_NORMAL:
  450. +                       elementName = "td"; //$NON-NLS-1$
  451. +                       break;
  452. +               case TABLE_ROW:
  453. +                       elementName = "tr"; //$NON-NLS-1$
  454. +                       break;
  455. +               case INFORMATION:
  456. +                       elementName = "important"; //$NON-NLS-1$
  457. +                       allowTitle = true;
  458. +                       break;
  459. +               case NOTE:
  460. +                       elementName = "note"; //$NON-NLS-1$
  461. +                       allowTitle = true;
  462. +                       break;
  463. +               case WARNING:
  464. +                       elementName = "warning"; //$NON-NLS-1$
  465. +                       allowTitle = true;
  466. +                       break;
  467. +               case TIP:
  468. +                       elementName = "tip"; //$NON-NLS-1$
  469. +                       allowTitle = true;
  470. +                       break;
  471. +               case PANEL:
  472. +                       elementName = "note"; // docbook has nothing better for 'note' //$NON-NLS-1$
  473. +                       allowTitle = true;
  474. +                       break;
  475. +               case DIV:
  476. +                       elementName = null;
  477. +                       break;
  478. +               default:
  479. +                       throw new IllegalStateException(type.name());
  480. +               }
  481. +
  482. +               int blockSize;
  483. +               if (elementName != null) {
  484. +                       blockSize = 1;
  485. +
  486. +                       if (previousBlock != null && previousBlock.closeElementsOnBlockStart) {
  487. +                               endBlockEntry(previousBlock);
  488. +                       }
  489. +
  490. +                       writer.writeStartElement(elementName);
  491. +                       applyAttributes(attributes);
  492. +
  493. +                       if (elementNames != null) {
  494. +                               for (String name : elementNames) {
  495. +                                       writer.writeStartElement(name);
  496. +                               }
  497. +                       }
  498. +
  499. +                       if (allowTitle && attributes.getTitle() != null) {
  500. +                               writer.writeStartElement("title"); //$NON-NLS-1$
  501. +                               writer.writeCharacters(attributes.getTitle());
  502. +                               writer.writeEndElement();
  503. +                       }
  504. +               } else {
  505. +                       blockSize = 0;
  506. +               }
  507. +               blockDescriptions.push(new BlockDescription(type, blockSize, elementNames, closeElementsOnBlockStart));
  508. +       }
  509. +
  510. +       @Override
  511. +       public void endBlock() {
  512. +               final BlockDescription blockDescription = blockDescriptions.pop();
  513. +               int size = blockDescription.size + blockDescription.entrySize;
  514. +               for (int x = 0; x < size; ++x) {
  515. +                       writer.writeEndElement();
  516. +               }
  517. +       }
  518. +
  519. +       private void endBlockEntry(BlockDescription blockDescription) {
  520. +               for (int x = 0; x < blockDescription.entrySize; ++x) {
  521. +                       writer.writeEndElement();
  522. +               }
  523. +               blockDescription.entrySize = 0;
  524. +       }
  525. +
  526. +       private void openBlockEntry(BlockDescription blockDescription, String[] entry) {
  527. +               for (String ent : entry) {
  528. +                       writer.writeStartElement(ent);
  529. +               }
  530. +               blockDescription.entrySize += entry.length;
  531. +       }
  532. +
  533. +       @Override
  534. +       public void beginHeading(int level, Attributes attributes) {
  535. +               closeSections(Math.max(level - 1, 0));
  536. +
  537. +               while (headingLevel < level) {
  538. +                       headingLevel++;
  539. +
  540. +                       writer.writeStartElement(headingLevel == 1 ? "chapter" : "section"); //$NON-NLS-1$ //$NON-NLS-2$
  541. +                       if (attributes != null) {
  542. +                               applyAttributes(attributes);
  543. +                               attributes = null;
  544. +                       }
  545. +               }
  546. +
  547. +               writer.writeStartElement("title"); //$NON-NLS-1$
  548. +
  549. +       }
  550. +
  551. +       @Override
  552. +       public void endHeading() {
  553. +               writer.writeEndElement(); // title
  554. +       }
  555. +
  556. +       @Override
  557. +       public void beginDocument() {
  558. +               baseInHead = false;
  559. +//             writer.writeStartDocument();
  560. +//             writer.writeDTD(doctype);
  561. +
  562. +//             writer.writeStartElement("book"); //$NON-NLS-1$
  563. +//             writer.writeStartElement("title"); //$NON-NLS-1$
  564. +//             if (bookTitle != null) {
  565. +//                     writer.writeCharacters(bookTitle);
  566. +//             }
  567. +//             writer.writeEndElement();
  568. +       }
  569. +
  570. +       @Override
  571. +       public void beginSpan(SpanType type, Attributes attributes) {
  572. +               ensureBlockElementsOpen();
  573. +               switch (type) {
  574. +               case BOLD:
  575. +               case STRONG:
  576. +                       writer.writeStartElement("emphasis"); //$NON-NLS-1$
  577. +                       writer.writeAttribute("role", "bold"); //$NON-NLS-1$ //$NON-NLS-2$
  578. +                       break;
  579. +               case CITATION:
  580. +                       writer.writeStartElement("citation"); //$NON-NLS-1$
  581. +                       break;
  582. +               case CODE:
  583. +                       writer.writeStartElement("code"); //$NON-NLS-1$
  584. +                       break;
  585. +               case DELETED:
  586. +                       writer.writeStartElement("emphasis"); //$NON-NLS-1$
  587. +                       writer.writeAttribute("role", "del"); //$NON-NLS-1$ //$NON-NLS-2$
  588. +                       break;
  589. +               case EMPHASIS:
  590. +                       writer.writeStartElement("emphasis"); //$NON-NLS-1$
  591. +                       break;
  592. +               case INSERTED:
  593. +                       writer.writeStartElement("emphasis"); //$NON-NLS-1$
  594. +                       writer.writeAttribute("role", "ins"); //$NON-NLS-1$ //$NON-NLS-2$
  595. +                       break;
  596. +               case UNDERLINED:
  597. +                       writer.writeStartElement("emphasis"); //$NON-NLS-1$
  598. +                       writer.writeAttribute("role", "underline"); //$NON-NLS-1$ //$NON-NLS-2$
  599. +                       break;
  600. +               case ITALIC:
  601. +                       writer.writeStartElement("emphasis"); //$NON-NLS-1$
  602. +                       writer.writeAttribute("role", "italic"); //$NON-NLS-1$ //$NON-NLS-2$
  603. +                       break;
  604. +               case QUOTE:
  605. +                       writer.writeStartElement("quote"); //$NON-NLS-1$
  606. +                       break;
  607. +               case SPAN:
  608. +                       writer.writeStartElement("phrase"); //$NON-NLS-1$
  609. +                       break;
  610. +               case SUBSCRIPT:
  611. +                       writer.writeStartElement("subscript"); //$NON-NLS-1$
  612. +                       break;
  613. +               case SUPERSCRIPT:
  614. +                       writer.writeStartElement("superscript"); //$NON-NLS-1$
  615. +                       break;
  616. +               case MONOSPACE:
  617. +                       writer.writeStartElement("literal"); //$NON-NLS-1$
  618. +                       break;
  619. +               case LINK: {
  620. +                       LinkAttributes linkAttributes = (LinkAttributes) attributes;
  621. +                       String href = linkAttributes.getHref();
  622. +                       if (href.startsWith("#")) { //$NON-NLS-1$
  623. +                               writer.writeStartElement("link"); //$NON-NLS-1$
  624. +                               if (href.length() > 1) {
  625. +                                       writer.writeAttribute("linkend", href.substring(1)); //$NON-NLS-1$
  626. +                               }
  627. +                       } else {
  628. +                               writer.writeStartElement("ulink"); //$NON-NLS-1$
  629. +                               writer.writeAttribute("url", href); //$NON-NLS-1$
  630. +                       }
  631. +               }
  632. +                       break;
  633. +               default:
  634. +                       Logger.getLogger(DocBookDocumentBuilder.class.getName()).warning("No docbook mapping for " + type); //$NON-NLS-1$
  635. +                       writer.writeStartElement("phrase"); //$NON-NLS-1$
  636. +                       break;
  637. +               }
  638. +               applyAttributes(attributes);
  639. +       }
  640. +
  641. +       private void applyAttributes(Attributes attributes) {
  642. +        if (attributes.getId() != null) {
  643. +            writer.writeAttribute("id", attributes.getId()); //$NON-NLS-1$
  644. +        }
  645. +        if (attributes.getLanguage() != null) {
  646. +            writer.writeAttribute("language", attributes.getLanguage()); //$NON-NLS-1$
  647. +        }
  648. +       }
  649. +
  650. +       @Override
  651. +       public void endDocument() {
  652. +               closeSections(0);
  653. +
  654. +               writeGlossaryAppendix();
  655. +
  656. +//             writer.writeEndElement(); // book
  657. +               writer.writeEndDocument();
  658. +
  659. +               acronyms.clear();
  660. +       }
  661. +
  662. +       private void closeSections(int toLevel) {
  663. +               if (toLevel < 0) {
  664. +                       toLevel = 0;
  665. +               }
  666. +               while (headingLevel > toLevel) {
  667. +                       writer.writeEndElement();
  668. +                       --headingLevel;
  669. +               }
  670. +       }
  671. +
  672. +       private void writeGlossaryAppendix() {
  673. +               if (!acronyms.isEmpty() && automaticGlossary) {
  674. +                       writer.writeStartElement("appendix"); //$NON-NLS-1$
  675. +                       writer.writeAttribute("id", "glossary"); //$NON-NLS-1$ //$NON-NLS-2$
  676. +                       writer.writeStartElement("title"); //$NON-NLS-1$
  677. +                       writer.writeAttribute("id", "glossary-end"); //$NON-NLS-1$ //$NON-NLS-2$
  678. +                       writer.writeCharacters("Glossary"); //$NON-NLS-1$
  679. +                       writer.writeEndElement(); // title
  680. +                       writer.writeStartElement("glosslist"); //$NON-NLS-1$
  681. +
  682. +                       for (Map.Entry<String, String> glossEntry : new TreeMap<String, String>(acronyms).entrySet()) {
  683. +
  684. +                               writer.writeStartElement("glossentry"); //$NON-NLS-1$
  685. +
  686. +                               writer.writeStartElement("glossterm"); //$NON-NLS-1$
  687. +                               writer.writeCharacters(glossEntry.getKey());
  688. +                               writer.writeEndElement(); // glossterm
  689. +
  690. +                               writer.writeStartElement("glossdef"); //$NON-NLS-1$
  691. +                               writer.writeStartElement("para"); //$NON-NLS-1$
  692. +                               writer.writeCharacters(glossEntry.getValue());
  693. +                               writer.writeEndElement(); // para
  694. +                               writer.writeEndElement(); // glossdef
  695. +
  696. +                               writer.writeEndElement(); // glossentry
  697. +                       }
  698. +                       writer.writeEndElement(); // glosslist
  699. +                       writer.writeEndElement(); // appendix
  700. +               }
  701. +       }
  702. +
  703. +       @Override
  704. +       public void endSpan() {
  705. +               writer.writeEndElement();
  706. +       }
  707. +
  708. +       @Override
  709. +       public void characters(String text) {
  710. +               ensureBlockElementsOpen();
  711. +               super.characters(text);
  712. +       }
  713. +
  714. +       @Override
  715. +       public void charactersUnescaped(String literal) {
  716. +               ensureBlockElementsOpen();
  717. +               // note: this *may* have HTML tags in it
  718. +               writer.writeLiteral(literal);
  719. +               //              Logger.getLogger(DocBookDocumentBuilder.class.getName()).warning("HTML literal not supported in DocBook");
  720. +       }
  721. +
  722. +       private void ensureBlockElementsOpen() {
  723. +               if (!blockDescriptions.isEmpty()) {
  724. +                       BlockDescription blockDescription = blockDescriptions.peek();
  725. +                       if (blockDescription.entrySize == 0 && blockDescription.nestedElementNames != null) {
  726. +                               openBlockEntry(blockDescription, blockDescription.nestedElementNames);
  727. +                       }
  728. +               }
  729. +       }
  730. +
  731. +       @Override
  732. +       public void entityReference(String entity) {
  733. +               ensureBlockElementsOpen();
  734. +               if (entity.startsWith("#")) { //$NON-NLS-1$
  735. +                       String numeric = entity.substring(1);
  736. +                       int base = 10;
  737. +                       if (numeric.startsWith("x")) { //$NON-NLS-1$
  738. +                               numeric = entity.substring(1);
  739. +                               base = 16;
  740. +                       }
  741. +                       int unicodeValue = Integer.parseInt(numeric, base);
  742. +                       if (entityReferenceToUnicode.contains(unicodeValue)) {
  743. +                               writer.writeCharacters("" + ((char) unicodeValue)); //$NON-NLS-1$
  744. +                               return;
  745. +                       }
  746. +               }
  747. +               writer.writeEntityRef(entity);
  748. +       }
  749. +
  750. +       @Override
  751. +       public void image(Attributes attributes, String url) {
  752. +               ensureBlockElementsOpen();
  753. +               String cssClass = attributes.getCssClass();
  754. +               boolean inlined = false;
  755. +               if (cssClass != null && CSS_CLASS_INLINE.matcher(cssClass).find()) {
  756. +                       inlined = true;
  757. +               }
  758. +               emitImage(attributes, url, inlined);
  759. +       }
  760. +
  761. +       private void emitImage(Attributes attributes, String url, boolean inline) {
  762. +               // see http://www.docbook.org/tdg/en/html/imagedata-x.html
  763. +               ensureBlockElementsOpen();
  764. +               writer.writeStartElement(inline ? "inlinemediaobject" : "mediaobject"); //$NON-NLS-1$ //$NON-NLS-2$
  765. +               applyAttributes(attributes);
  766. +               writer.writeStartElement("imageobject"); //$NON-NLS-1$
  767. +               writer.writeEmptyElement("imagedata"); //$NON-NLS-1$
  768. +               writer.writeAttribute("fileref", makeUrlAbsolute(url)); //$NON-NLS-1$
  769. +               String cssStyle = attributes.getCssStyle();
  770. +               if (cssStyle != null) {
  771. +                       String width = null;
  772. +                       String depth = null;
  773. +                       Iterator<CssRule> ruleIterator = new CssParser().createRuleIterator(cssStyle);
  774. +                       while (ruleIterator.hasNext()) {
  775. +                               CssRule rule = ruleIterator.next();
  776. +                               if ("width".equals(rule.name)) { //$NON-NLS-1$
  777. +                                       width = rule.value;
  778. +                               } else if ("height".equals(rule.name)) { //$NON-NLS-1$
  779. +                                       depth = rule.value;
  780. +                               }
  781. +                       }
  782. +                       if (width != null) {
  783. +                               Matcher matcher = PERCENTAGE.matcher(width);
  784. +                               if (matcher.matches()) {
  785. +                                       writer.writeAttribute("scale", matcher.group(1)); //$NON-NLS-1$
  786. +                               } else {
  787. +                                       writer.writeAttribute("width", width); //$NON-NLS-1$
  788. +                                       if (depth != null) {
  789. +                                               writer.writeAttribute("depth", depth); //$NON-NLS-1$
  790. +                                       }
  791. +                               }
  792. +                       }
  793. +               }
  794. +               writer.writeEndElement(); // imageobject
  795. +               writer.writeEndElement(); // inlinemediaobject or mediaobject
  796. +       }
  797. +
  798. +       @Override
  799. +       public void imageLink(Attributes linkAttributes, final Attributes imageAttributes, String href,
  800. +                       final String imageUrl) {
  801. +               link(linkAttributes, href, new ContentEmitter() {
  802. +                       public void emit() {
  803. +                               emitImage(imageAttributes, imageUrl, true);
  804. +                       }
  805. +               });
  806. +       }
  807. +
  808. +       @Override
  809. +       public void lineBreak() {
  810. +               ensureBlockElementsOpen();
  811. +               // no equivalent in DocBook.
  812. +               characters("\n"); //$NON-NLS-1$
  813. +       }
  814. +
  815. +       private BlockDescription findBlockDescription(BlockType type) {
  816. +               for (int x = blockDescriptions.size() - 1; x >= 0; --x) {
  817. +                       BlockDescription blockDescription = blockDescriptions.get(x);
  818. +                       if (blockDescription.type == type) {
  819. +                               return blockDescription;
  820. +                       }
  821. +               }
  822. +               return null;
  823. +       }
  824. +
  825. +       private static class BlockDescription {
  826. +               BlockType type;
  827. +
  828. +               int size;
  829. +
  830. +               int entrySize; // the size of an entry, if it is open, otherwise 0
  831. +
  832. +               final String[] nestedElementNames;
  833. +
  834. +               final boolean closeElementsOnBlockStart;
  835. +
  836. +               public BlockDescription(DocumentBuilder.BlockType type, int size, String[] nestedElementNames, boolean closeElementsOnBlockStart) {
  837. +                       this.size = size;
  838. +                       this.entrySize = nestedElementNames == null ? 0 : nestedElementNames.length;
  839. +                       this.type = type;
  840. +                       this.nestedElementNames = nestedElementNames;
  841. +                       this.closeElementsOnBlockStart = closeElementsOnBlockStart;
  842. +               }
  843. +       }
  844. +
  845. +       /**
  846. +        * Indicate if this builder should generate an automatic glossary if acronyms are used. When the automatic glossary
  847. +        * is enabled and acronyms are used in the document, then an <code>appendix</code> with title 'Glossary' is added to
  848. +        * the document, with a <code>glosslist</code> generated for all of the acronyms that appear in the document. The
  849. +        * default is true.
  850. +        */
  851. +       public boolean isAutomaticGlossary() {
  852. +               return automaticGlossary;
  853. +       }
  854. +
  855. +       /**
  856. +        * Indicate if this builder should generate an automatic glossary if acronyms are used. When the automatic glossary
  857. +        * is enabled and acronyms are used in the document, then an <code>appendix</code> with title 'Glossary' is added to
  858. +        * the document, with a <code>glosslist</code> generated for all of the acronyms that appear in the document. The
  859. +        * default is true.
  860. +        */
  861. +       public void setAutomaticGlossary(boolean automaticGlossary) {
  862. +               this.automaticGlossary = automaticGlossary;
  863. +       }
  864. +
  865. +}
  866. Index: src/main/java/org/apache/servicemix/docs/confluence/ConfluenceConverter.java
  867. ===================================================================
  868. --- src/main/java/org/apache/servicemix/docs/confluence/ConfluenceConverter.java        (revision 954753)
  869. +++ src/main/java/org/apache/servicemix/docs/confluence/ConfluenceConverter.java        (working copy)
  870. @@ -%ld,%ld +%ld,%ld @@
  871.  import java.io.PrintStream;
  872.  import java.io.PrintWriter;
  873.  import java.io.Reader;
  874. +import java.io.StringWriter;
  875.  import java.io.Writer;
  876.  import java.util.HashMap;
  877.  import java.util.LinkedHashMap;
  878. @@ -%ld,%ld +%ld,%ld @@
  879.  import java.util.regex.Matcher;
  880.  import java.util.regex.Pattern;
  881.  
  882. +import org.eclipse.mylyn.wikitext.core.parser.MarkupParser;
  883. +import org.eclipse.mylyn.wikitext.core.parser.markup.MarkupLanguage;
  884. +
  885.  /**
  886.   * Represents a document in Confluence wiki markup
  887.   */
  888. @@ -%ld,%ld +%ld,%ld @@
  889.      }
  890.  
  891.      public void convert() throws IOException {
  892. -        String line = null;
  893. +        MarkupLanguage markupLanguage = new ConfluenceLanguage();
  894. +        StringWriter writer = new StringWriter();
  895. +        DocBookDocumentBuilder builder = new DocBookDocumentBuilder(writer);
  896. +        MarkupParser parser = new MarkupParser();
  897. +        parser.setMarkupLanguage(markupLanguage);
  898. +        parser.setBuilder(builder);
  899. +        parser.parse(reader);
  900. +        writer.flush();
  901. +        String out = writer.toString();
  902. +//        int beg = out.indexOf("<chapter");
  903. +//        int end = out.indexOf("</book>");
  904. +//        out = out.substring(beg, end);
  905. +        this.writer.write(out);
  906.  
  907. -        while ((line = reader.readLine()) != null) {
  908. -            String trimmed = line.trim();
  909. -            if (CHAPTER_OR_SECTION.matcher(line).matches()) {
  910. -                writeChapterOrSection(line);
  911. -            } else if (trimmed.startsWith("!") && trimmed.endsWith("!")) {
  912. -                writeImage(line);
  913. -            } else if (trimmed.startsWith("||") && trimmed.endsWith("||")) {
  914. -                writeTableHeader(line);
  915. -            } else if (trimmed.startsWith("|") && trimmed.endsWith("|")) {
  916. -                writeTableBody(line);
  917. -            } else if (line.startsWith("*")) {
  918. -                writeBulletList(line);
  919. -            } else if (line.startsWith("#")) {
  920. -                writeNumberedList(line);
  921. -            } else if (line.startsWith("-")) {
  922. -                writeGlossaryList(line);
  923. -            } else if (trimmed.startsWith("{code")) {
  924. -                writeProgramListing(line);
  925. -            } else {
  926. -                writePara(line);
  927. -            }
  928. -        }
  929. +//        String line = null;
  930. +//        while ((line = reader.readLine()) != null) {
  931. +//            String trimmed = line.trim();
  932. +//            if (CHAPTER_OR_SECTION.matcher(line).matches()) {
  933. +//                writeChapterOrSection(line);
  934. +//            } else if (trimmed.startsWith("!") && trimmed.endsWith("!")) {
  935. +//                writeImage(line);
  936. +//            } else if (trimmed.startsWith("||") && trimmed.endsWith("||")) {
  937. +//                writeTableHeader(line);
  938. +//            } else if (trimmed.startsWith("|") && trimmed.endsWith("|")) {
  939. +//                writeTableBody(line);
  940. +//            } else if (line.startsWith("* ")) {
  941. +//                writeBulletList(line);
  942. +//            } else if (line.startsWith("# ")) {
  943. +//                writeNumberedList(line);
  944. +//            } else if (line.startsWith("- ")) {
  945. +//                writeGlossaryList(line);
  946. +//            } else if (trimmed.startsWith("{code")) {
  947. +//                writeProgramListing(line);
  948. +//            } else if (trimmed.startsWith("{tip")) {
  949. +//                writeTip(line);
  950. +//            } else if (trimmed.startsWith("{warning")) {
  951. +//                writeWarning(line);
  952. +//            } else if (trimmed.startsWith("{info")) {
  953. +//                writeInfo(line);
  954. +//            } else {
  955. +//                writePara(line);
  956. +//            }
  957. +//        }
  958.  
  959.          // let's close everying that was opened
  960. -        closeElements();
  961. +//        closeElements();
  962.  
  963.      }
  964.  
  965. @@ -%ld,%ld +%ld,%ld @@
  966.          writeRaw("]]></programlisting>");
  967.      }
  968.  
  969. +    private void writeTip(String line) throws IOException {
  970. +        writeRaw("<programlisting><![CDATA[");
  971. +        while ((line = reader.readLine()) != null && !line.equals("{tip}")) {
  972. +            writeRaw(line, "");
  973. +        }
  974. +        writeRaw("]]></programlisting>");
  975. +    }
  976. +
  977. +    private void writeWarning(String line) throws IOException {
  978. +        writeRaw("<programlisting><![CDATA[");
  979. +        while ((line = reader.readLine()) != null && !line.equals("{warning}")) {
  980. +            writeRaw(line, "");
  981. +        }
  982. +        writeRaw("]]></programlisting>");
  983. +    }
  984. +
  985. +    private void writeInfo(String line) throws IOException {
  986. +        writeRaw("<programlisting><![CDATA[");
  987. +        while ((line = reader.readLine()) != null && !line.equals("{info}")) {
  988. +            writeRaw(line, "");
  989. +        }
  990. +        writeRaw("]]></programlisting>");
  991. +    }
  992. +
  993.      private void writePara(String line) {
  994.          // a <para/> shouldn't
  995.          ensureClosed("informaltable");
  996. @@ -%ld,%ld +%ld,%ld @@
  997.          replacements.put("&", "&amp;");
  998.          replacements.put("<", "&lt;");
  999.          replacements.put(">", "&gt;");
  1000. -        replacements.put("\\{\\{(.*)\\}\\}", "<code>$1</code>");
  1001. +        replacements.put("\\{\\{([^\\}]*)\\}\\}", "<code>$1</code>");
  1002.  
  1003.          replacements.put("\\*([^*]*)\\*", "<emphasis role=\\\"bold\\\">$1</emphasis>");
  1004. +        replacements.put("_([^_]*)_", "<emphasis role=\\\"italics\\\">$1</emphasis>");
  1005.  
  1006.          replacements.put("([^\\\\])\\[([^\\|\\]]*)\\|([^\\]]*)\\]", "$1<ulink url=\\\"$3\\\">$2</ulink>");
  1007.          replacements.put("^\\[([^\\|\\]]*)\\|([^\\]]*)\\]", "<ulink url=\\\"$2\\\">$1</ulink>");
  1008. Index: pom.xml
  1009. ===================================================================
  1010. --- pom.xml     (revision 954751)
  1011. +++ pom.xml     (working copy)
  1012. @@ -%ld,%ld +%ld,%ld @@
  1013.        <version>${junit.version}</version>
  1014.        <scope>test</scope>
  1015.      </dependency>
  1016. +      <dependency>
  1017. +          <groupId>org.eclipse.mylyn.wikitext</groupId>
  1018. +          <artifactId>confluence.core</artifactId>
  1019. +          <version>1.3.0.v20100608-0100-e3x</version>
  1020. +      </dependency>
  1021. +      <dependency>
  1022. +          <groupId>org.eclipse.mylyn.wikitext</groupId>
  1023. +          <artifactId>core</artifactId>
  1024. +          <version>1.3.0.v20100608-0100-e3x</version>
  1025. +      </dependency>
  1026.    </dependencies>
  1027.  </project>