001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.wiki.engines.antlrwiki.translator;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.repository.model.FileEntry;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.kernel.util.TreeNode;
025    import com.liferay.portal.parsers.creole.ast.CollectionNode;
026    import com.liferay.portal.parsers.creole.ast.HeadingNode;
027    import com.liferay.portal.parsers.creole.ast.ImageNode;
028    import com.liferay.portal.parsers.creole.ast.WikiPageNode;
029    import com.liferay.portal.parsers.creole.ast.extension.TableOfContentsNode;
030    import com.liferay.portal.parsers.creole.ast.link.LinkNode;
031    import com.liferay.portal.parsers.creole.visitor.impl.XhtmlTranslationVisitor;
032    import com.liferay.portlet.wiki.NoSuchPageException;
033    import com.liferay.portlet.wiki.engines.antlrwiki.translator.internal.UnformattedHeadingTextVisitor;
034    import com.liferay.portlet.wiki.engines.antlrwiki.translator.internal.UnformattedLinksTextVisitor;
035    import com.liferay.portlet.wiki.model.WikiPage;
036    import com.liferay.portlet.wiki.service.WikiPageLocalServiceUtil;
037    
038    import java.util.List;
039    
040    import javax.portlet.PortletURL;
041    
042    /**
043     * @author Miguel Pastor
044     */
045    public class XhtmlTranslator extends XhtmlTranslationVisitor {
046    
047            public String translate(
048                    WikiPage page, PortletURL viewPageURL, PortletURL editPageURL,
049                    String attachmentURLPrefix, WikiPageNode wikiPageNode) {
050    
051                    _page = page;
052                    _viewPageURL = viewPageURL;
053                    _editPageURL = editPageURL;
054                    _attachmentURLPrefix = attachmentURLPrefix;
055                    _rootWikiPageNode = wikiPageNode;
056    
057                    return super.translate(wikiPageNode);
058            }
059    
060            @Override
061            public void visit(HeadingNode headingNode) {
062                    append("<h");
063                    append(headingNode.getLevel());
064    
065                    String unformattedText = getUnformattedHeadingText(headingNode);
066    
067                    String markup = getHeadingMarkup(_page.getTitle(), unformattedText);
068    
069                    append(" id=\"");
070                    append(markup);
071                    append("\">");
072    
073                    traverse(headingNode.getChildASTNodes());
074    
075                    append("<a class=\"hashlink\" href=\"");
076    
077                    if (_viewPageURL != null) {
078                            append(_viewPageURL.toString());
079                    }
080    
081                    append(StringPool.POUND);
082                    append(markup);
083                    append("\">#</a></h");
084                    append(headingNode.getLevel());
085                    append(">");
086            }
087    
088            @Override
089            public void visit(ImageNode imageNode) {
090                    append("<img");
091    
092                    if (imageNode.hasAltCollectionNode()) {
093                            append(" alt=\"");
094    
095                            CollectionNode altCollectionNode = imageNode.getAltNode();
096    
097                            traverse(altCollectionNode.getASTNodes());
098    
099                            append(StringPool.QUOTE);
100                    }
101    
102                    append(" src=\"");
103    
104                    if (imageNode.isAbsoluteLink()) {
105                            append(imageNode.getLink());
106                    }
107                    else {
108                            append(_attachmentURLPrefix);
109                            append(imageNode.getLink());
110                    }
111    
112                    append("\" />");
113            }
114    
115            @Override
116            public void visit(LinkNode linkNode) {
117                    append("<a href=\"");
118    
119                    appendHref(linkNode);
120    
121                    append("\">");
122    
123                    if (linkNode.hasAltCollectionNode()) {
124                            CollectionNode altCollectionNode = linkNode.getAltCollectionNode();
125    
126                            traverse(altCollectionNode.getASTNodes());
127                    }
128                    else {
129                            append(HtmlUtil.escape(linkNode.getLink()));
130                    }
131    
132                    append("</a>");
133            }
134    
135            @Override
136            public void visit(TableOfContentsNode tableOfContentsNode) {
137                    TableOfContentsVisitor tableOfContentsVisitor =
138                            new TableOfContentsVisitor();
139    
140                    TreeNode<HeadingNode> tableOfContents = tableOfContentsVisitor.compose(
141                            _rootWikiPageNode);
142    
143                    append("<div class=\"toc\">");
144                    append("<div class=\"collapsebox\">");
145                    append("<h4>");
146    
147                    String title = tableOfContentsNode.getTitle();
148    
149                    if (title == null) {
150                            title = "Table of Contents";
151                    }
152    
153                    append(title);
154    
155                    append(StringPool.NBSP);
156                    append("<a class=\"toc-trigger\" href=\"javascript:;\">[-]</a></h4>");
157                    append("<div class=\"toc-index\">");
158    
159                    appendTableOfContents(tableOfContents, 1);
160    
161                    append("</div>");
162                    append("</div>");
163                    append("</div>");
164            }
165    
166            protected void appendAbsoluteHref(LinkNode linkNode) {
167                    append(HtmlUtil.escape(linkNode.getLink()));
168            }
169    
170            protected void appendHref(LinkNode linkNode) {
171                    if (linkNode.getLink() == null) {
172                            UnformattedLinksTextVisitor unformattedLinksTextVisitor =
173                                    new UnformattedLinksTextVisitor();
174    
175                            linkNode.setLink(
176                                    unformattedLinksTextVisitor.getUnformattedText(linkNode));
177                    }
178    
179                    if (linkNode.isAbsoluteLink()) {
180                            appendAbsoluteHref(linkNode);
181                    }
182                    else {
183                            appendWikiHref(linkNode);
184                    }
185            }
186    
187            protected void appendTableOfContents(
188                    TreeNode<HeadingNode> tableOfContents, int depth) {
189    
190                    List<TreeNode<HeadingNode>> treeNodes = tableOfContents.getChildNodes();
191    
192                    if ((treeNodes == null) || treeNodes.isEmpty()) {
193                            return;
194                    }
195    
196                    append("<ol>");
197    
198                    for (TreeNode<HeadingNode> treeNode : treeNodes) {
199                            append("<li class=\"toc-level-");
200                            append(depth);
201                            append("\">");
202    
203                            HeadingNode headingNode = treeNode.getValue();
204    
205                            String content = getUnformattedHeadingText(headingNode);
206    
207                            append("<a class=\"wikipage\" href=\"");
208    
209                            if (_viewPageURL != null) {
210                                    append(_viewPageURL.toString());
211                            }
212    
213                            append(StringPool.POUND);
214                            append(getHeadingMarkup(_page.getTitle(), content));
215                            append("\">");
216                            append(content);
217                            append("</a>");
218    
219                            appendTableOfContents(treeNode, depth + 1);
220    
221                            append("</li>");
222                    }
223    
224                    append("</ol>");
225            }
226    
227            protected void appendWikiHref(LinkNode linkNode) {
228                    WikiPage page = null;
229    
230                    try {
231                            page = WikiPageLocalServiceUtil.getPage(
232                                    _page.getNodeId(), linkNode.getLink());
233                    }
234                    catch (NoSuchPageException nspe) {
235                    }
236                    catch (Exception e) {
237                            _log.error(e, e);
238                    }
239    
240                    String attachmentLink = searchLinkInAttachments(linkNode);
241    
242                    if (attachmentLink != null) {
243    
244                            // Attachment links take precedence over pages
245    
246                            append(_attachmentURLPrefix + attachmentLink);
247    
248                            return;
249                    }
250    
251                    String pageTitle = linkNode.getLink();
252    
253                    if ((page != null) && (_viewPageURL != null)) {
254                            _viewPageURL.setParameter("title", pageTitle);
255    
256                            append(_viewPageURL.toString());
257    
258                            _viewPageURL.setParameter("title", _page.getTitle());
259                    }
260                    else if (_editPageURL != null) {
261                            _editPageURL.setParameter("title", pageTitle);
262    
263                            append(_editPageURL.toString());
264    
265                            _editPageURL.setParameter("title", _page.getTitle());
266                    }
267            }
268    
269            protected String getHeadingMarkup(String prefix, String text) {
270                    StringBundler sb = new StringBundler(5);
271    
272                    sb.append(_HEADING_ANCHOR_PREFIX);
273                    sb.append(prefix);
274                    sb.append(StringPool.DASH);
275                    sb.append(text.trim());
276    
277                    return StringUtil.replace(
278                            sb.toString(), StringPool.SPACE, StringPool.PLUS);
279            }
280    
281            protected String getUnformattedHeadingText(HeadingNode headingNode) {
282                    UnformattedHeadingTextVisitor unformattedHeadingTextVisitor =
283                            new UnformattedHeadingTextVisitor();
284    
285                    return unformattedHeadingTextVisitor.getUnformattedText(headingNode);
286            }
287    
288            protected String searchLinkInAttachments(LinkNode linkNode) {
289                    try {
290                            for (FileEntry fileEntry : _page.getAttachmentsFileEntries()) {
291                                    String title = fileEntry.getTitle();
292    
293                                    if (title.equals(linkNode.getLink())) {
294                                            return title;
295                                    }
296                            }
297                    }
298                    catch (Exception e) {
299                    }
300    
301                    return null;
302            }
303    
304            private static final String _HEADING_ANCHOR_PREFIX = "section-";
305    
306            private static Log _log = LogFactoryUtil.getLog(XhtmlTranslator.class);
307    
308            private String _attachmentURLPrefix;
309            private PortletURL _editPageURL;
310            private WikiPage _page;
311            private WikiPageNode _rootWikiPageNode;
312            private PortletURL _viewPageURL;
313    
314    }