| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -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; |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 16 | |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 17 | import com.google.gitiles.doc.GitilesMarkdown; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 18 | import com.google.gitiles.doc.MarkdownConfig; |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 19 | import com.google.gitiles.doc.MarkdownToHtml; |
| 20 | import com.google.template.soy.data.SanitizedContent; |
| Dave Borowitz | 3b744b1 | 2016-08-19 16:11:10 -0400 | [diff] [blame] | 21 | import java.io.IOException; |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 22 | import org.eclipse.jgit.errors.CorruptObjectException; |
| 23 | import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 24 | import org.eclipse.jgit.errors.MissingObjectException; |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 25 | import org.eclipse.jgit.lib.Constants; |
| 26 | import org.eclipse.jgit.lib.FileMode; |
| 27 | import org.eclipse.jgit.lib.ObjectId; |
| 28 | import org.eclipse.jgit.lib.ObjectReader; |
| 29 | import org.eclipse.jgit.revwalk.RevTree; |
| 30 | import org.eclipse.jgit.treewalk.TreeWalk; |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 31 | import org.slf4j.Logger; |
| 32 | import org.slf4j.LoggerFactory; |
| 33 | |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 34 | class ReadmeHelper { |
| 35 | private static final Logger log = LoggerFactory.getLogger(ReadmeHelper.class); |
| 36 | |
| 37 | private final ObjectReader reader; |
| 38 | private final GitilesView view; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 39 | private final MarkdownConfig config; |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 40 | private final RevTree rootTree; |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 41 | private final String requestUri; |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 42 | |
| 43 | private String readmePath; |
| 44 | private ObjectId readmeId; |
| 45 | |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 46 | ReadmeHelper( |
| 47 | ObjectReader reader, |
| 48 | GitilesView view, |
| 49 | MarkdownConfig config, |
| 50 | RevTree rootTree, |
| 51 | String requestUri) { |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 52 | this.reader = reader; |
| 53 | this.view = view; |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 54 | this.config = config; |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 55 | this.rootTree = rootTree; |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 56 | this.requestUri = requestUri; |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| Han-Wen Nienhuys | c0200f6 | 2016-05-02 17:34:51 +0200 | [diff] [blame] | 59 | void scanTree(RevTree tree) |
| 60 | throws MissingObjectException, IncorrectObjectTypeException, CorruptObjectException, |
| 61 | IOException { |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 62 | if (config.render) { |
| David Ostrovsky | 5685e57 | 2016-12-18 15:31:39 +0100 | [diff] [blame] | 63 | try (TreeWalk tw = new TreeWalk(reader)) { |
| 64 | tw.setRecursive(false); |
| 65 | tw.addTree(tree); |
| 66 | while (tw.next() && !isPresent()) { |
| 67 | considerEntry(tw); |
| 68 | } |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void considerEntry(TreeWalk tw) { |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 74 | if (config.render |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 75 | && FileMode.REGULAR_FILE.equals(tw.getRawMode(0)) |
| 76 | && isReadmeFile(tw.getNameString())) { |
| 77 | readmePath = tw.getPathString(); |
| 78 | readmeId = tw.getObjectId(0); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | boolean isPresent() { |
| 83 | return readmeId != null; |
| 84 | } |
| 85 | |
| 86 | String getPath() { |
| 87 | return readmePath; |
| 88 | } |
| 89 | |
| 90 | SanitizedContent render() { |
| 91 | try { |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 92 | byte[] raw = reader.open(readmeId, Constants.OBJ_BLOB).getCachedBytes(config.inputLimit); |
| 93 | return MarkdownToHtml.builder() |
| 94 | .setConfig(config) |
| 95 | .setGitilesView(view) |
| Shawn Pearce | c68ad0b | 2016-05-28 16:52:47 -0700 | [diff] [blame] | 96 | .setRequestUri(requestUri) |
| Shawn Pearce | 47fd656 | 2016-05-28 14:15:15 -0700 | [diff] [blame] | 97 | .setFilePath(readmePath) |
| 98 | .setReader(reader) |
| 99 | .setRootTree(rootTree) |
| 100 | .build() |
| 101 | .toSoyHtml(GitilesMarkdown.parse(raw)); |
| Shawn Pearce | 08e38f2 | 2016-05-24 16:40:18 -0700 | [diff] [blame] | 102 | } catch (RuntimeException | IOException err) { |
| 103 | log.error( |
| 104 | String.format( |
| 105 | "error rendering %s/%s %s:%s", |
| Dave Borowitz | 40255d5 | 2016-08-19 16:16:22 -0400 | [diff] [blame] | 106 | view.getHostName(), view.getRepositoryName(), view.getRevision(), readmePath), |
| Shawn Pearce | 08e38f2 | 2016-05-24 16:40:18 -0700 | [diff] [blame] | 107 | err); |
| Shawn Pearce | 45e8375 | 2015-02-20 17:59:05 -0800 | [diff] [blame] | 108 | return null; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** True if the file is the default markdown file to render in tree view. */ |
| 113 | private static boolean isReadmeFile(String name) { |
| 114 | return name.equalsIgnoreCase("README.md"); |
| 115 | } |
| 116 | } |