| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.gitiles.doc; |
| 16 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 17 | import com.google.common.base.CharMatcher; |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 18 | import com.google.common.base.Strings; |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 19 | import org.commonmark.node.Heading; |
| 20 | import org.commonmark.node.Node; |
| 21 | import org.commonmark.node.Text; |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 22 | |
| Shawn Pearce | 108599e | 2015-02-11 13:28:37 -0800 | [diff] [blame] | 23 | class MarkdownUtil { |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 24 | /** Combine child nodes as string; this must be escaped for HTML. */ |
| Shawn Pearce | 108599e | 2015-02-11 13:28:37 -0800 | [diff] [blame] | 25 | static String getInnerText(Node node) { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 26 | if (node == null || node.getFirstChild() == null) { |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 27 | return null; |
| 28 | } |
| 29 | |
| 30 | StringBuilder b = new StringBuilder(); |
| 31 | appendTextFromChildren(b, node); |
| 32 | return Strings.emptyToNull(b.toString().trim()); |
| 33 | } |
| 34 | |
| 35 | private static void appendTextFromChildren(StringBuilder b, Node node) { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 36 | for (Node c = node.getFirstChild(); c != null; c = c.getNext()) { |
| 37 | if (c instanceof Text) { |
| 38 | b.append(((Text) c).getLiteral()); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 39 | } else { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 40 | appendTextFromChildren(b, c); |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 45 | static String getTitle(Node node) { |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 46 | if (node instanceof Heading) { |
| 47 | if (((Heading) node).getLevel() == 1) { |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 48 | return getInnerText(node); |
| 49 | } |
| 50 | return null; |
| 51 | } |
| 52 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 53 | for (Node c = node.getFirstChild(); c != null; c = c.getNext()) { |
| 54 | String title = getTitle(c); |
| Shawn Pearce | 374f184 | 2015-02-10 15:36:54 -0800 | [diff] [blame] | 55 | if (title != null) { |
| 56 | return title; |
| 57 | } |
| 58 | } |
| 59 | return null; |
| 60 | } |
| 61 | |
| Shawn Pearce | 12c8fab | 2016-05-15 16:55:21 -0700 | [diff] [blame] | 62 | static void trimPreviousWhitespace(Node node) { |
| 63 | Node prev = node.getPrevious(); |
| 64 | if (prev instanceof Text) { |
| 65 | Text prevText = (Text) prev; |
| 66 | String s = prevText.getLiteral(); |
| 67 | prevText.setLiteral(CharMatcher.whitespace().trimTrailingFrom(s)); |
| 68 | } |
| 69 | } |
| 70 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 71 | private MarkdownUtil() {} |
| Shawn Pearce | c954998 | 2015-02-11 13:09:01 -0800 | [diff] [blame] | 72 | } |