blob: 9a6bb821a29ca651dedeac7e5fe3306bc5026b53 [file] [log] [blame]
Shawn Pearcec9549982015-02-11 13:09:01 -08001// 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
15package com.google.gitiles.doc;
16
Shawn Pearce12c8fab2016-05-15 16:55:21 -070017import com.google.common.base.CharMatcher;
Shawn Pearcec9549982015-02-11 13:09:01 -080018import com.google.common.base.Strings;
Shawn Pearce12c8fab2016-05-15 16:55:21 -070019import org.commonmark.node.Heading;
20import org.commonmark.node.Node;
21import org.commonmark.node.Text;
Shawn Pearcec9549982015-02-11 13:09:01 -080022
Shawn Pearce108599e2015-02-11 13:28:37 -080023class MarkdownUtil {
Shawn Pearcec9549982015-02-11 13:09:01 -080024 /** Combine child nodes as string; this must be escaped for HTML. */
Shawn Pearce108599e2015-02-11 13:28:37 -080025 static String getInnerText(Node node) {
Shawn Pearce12c8fab2016-05-15 16:55:21 -070026 if (node == null || node.getFirstChild() == null) {
Shawn Pearcec9549982015-02-11 13:09:01 -080027 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 Pearce12c8fab2016-05-15 16:55:21 -070036 for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
37 if (c instanceof Text) {
38 b.append(((Text) c).getLiteral());
Shawn Pearcec9549982015-02-11 13:09:01 -080039 } else {
Shawn Pearce12c8fab2016-05-15 16:55:21 -070040 appendTextFromChildren(b, c);
Shawn Pearcec9549982015-02-11 13:09:01 -080041 }
42 }
43 }
44
Shawn Pearce374f1842015-02-10 15:36:54 -080045 static String getTitle(Node node) {
Shawn Pearce12c8fab2016-05-15 16:55:21 -070046 if (node instanceof Heading) {
47 if (((Heading) node).getLevel() == 1) {
Shawn Pearce374f1842015-02-10 15:36:54 -080048 return getInnerText(node);
49 }
50 return null;
51 }
52
Shawn Pearce12c8fab2016-05-15 16:55:21 -070053 for (Node c = node.getFirstChild(); c != null; c = c.getNext()) {
54 String title = getTitle(c);
Shawn Pearce374f1842015-02-10 15:36:54 -080055 if (title != null) {
56 return title;
57 }
58 }
59 return null;
60 }
61
Shawn Pearce12c8fab2016-05-15 16:55:21 -070062 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 Nienhuysc0200f62016-05-02 17:34:51 +020071 private MarkdownUtil() {}
Shawn Pearcec9549982015-02-11 13:09:01 -080072}