diff options
author | James Moger <james.moger@gitblit.com> | 2013-11-16 11:42:06 -0500 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2013-11-26 16:07:04 -0500 |
commit | 99d0d4fd66f3490b61c700065b7d16bc4e73f226 (patch) | |
tree | 0e5d8bd9e7b14905ab6ea71e8bf6253345415a70 /src/main/java/com/gitblit/utils | |
parent | a7a0b8ea01dca14602fdb49047d987c36461e861 (diff) | |
download | gitblit-99d0d4fd66f3490b61c700065b7d16bc4e73f226.tar.gz gitblit-99d0d4fd66f3490b61c700065b7d16bc4e73f226.zip |
Eliminate nearly all direct GitBlit singleton references in Wicket
This is the first step towards modularization and injection. All
direct references to the GitBlit singleton within the Wicket pages
and panels have been replaced to proxy methods in the GitBlitWebApp
singleton. There are still two Wicket classes which rely on the
GitBlit singleton; those require manual instantiation (servlet 3).
Change-Id: I0cdbbcf87959d590c446c99abb09c07c87c737bc
Diffstat (limited to 'src/main/java/com/gitblit/utils')
-rw-r--r-- | src/main/java/com/gitblit/utils/MessageProcessor.java | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/main/java/com/gitblit/utils/MessageProcessor.java b/src/main/java/com/gitblit/utils/MessageProcessor.java new file mode 100644 index 00000000..58493de4 --- /dev/null +++ b/src/main/java/com/gitblit/utils/MessageProcessor.java @@ -0,0 +1,126 @@ +/* + * Copyright 2013 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.gitblit.utils; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.gitblit.IStoredSettings; +import com.gitblit.Keys; +import com.gitblit.models.RepositoryModel; + +public class MessageProcessor { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private final IStoredSettings settings; + + public MessageProcessor(IStoredSettings settings) { + this.settings = settings; + } + + /** + * Returns an html version of the commit message with any global or + * repository-specific regular expression substitution applied. + * + * This method uses the preferred renderer to transform the commit message. + * + * @param repository + * @param text + * @return html version of the commit message + */ + public String processCommitMessage(RepositoryModel repository, String text) { + switch (repository.commitMessageRenderer) { + case MARKDOWN: + try { + String prepared = processCommitMessageRegex(repository.name, text); + return MarkdownUtils.transformMarkdown(prepared); + } catch (Exception e) { + logger.error("Failed to render commit message as markdown", e); + } + break; + default: + // noop + break; + } + + return processPlainCommitMessage(repository.name, text); + } + + /** + * Returns an html version of the commit message with any global or + * repository-specific regular expression substitution applied. + * + * This method assumes the commit message is plain text. + * + * @param repositoryName + * @param text + * @return html version of the commit message + */ + public String processPlainCommitMessage(String repositoryName, String text) { + String html = StringUtils.escapeForHtml(text, false); + html = processCommitMessageRegex(repositoryName, html); + return StringUtils.breakLinesForHtml(html); + + } + + /** + * Apply globally or per-repository specified regex substitutions to the + * commit message. + * + * @param repositoryName + * @param text + * @return the processed commit message + */ + protected String processCommitMessageRegex(String repositoryName, String text) { + Map<String, String> map = new HashMap<String, String>(); + // global regex keys + if (settings.getBoolean(Keys.regex.global, false)) { + for (String key : settings.getAllKeys(Keys.regex.global)) { + if (!key.equals(Keys.regex.global)) { + String subKey = key.substring(key.lastIndexOf('.') + 1); + map.put(subKey, settings.getString(key, "")); + } + } + } + + // repository-specific regex keys + List<String> keys = settings.getAllKeys(Keys.regex._ROOT + "." + + repositoryName.toLowerCase()); + for (String key : keys) { + String subKey = key.substring(key.lastIndexOf('.') + 1); + map.put(subKey, settings.getString(key, "")); + } + + for (Entry<String, String> entry : map.entrySet()) { + String definition = entry.getValue().trim(); + String[] chunks = definition.split("!!!"); + if (chunks.length == 2) { + text = text.replaceAll(chunks[0], chunks[1]); + } else { + logger.warn(entry.getKey() + + " improperly formatted. Use !!! to separate match from replacement: " + + definition); + } + } + return text; + } +} |