This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 129
Process markdown cloud links for PHP #1091
Merged
michaelbausor
merged 13 commits into
googleapis:master
from
michaelbausor:php-markdown-docs
May 26, 2017
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
54fa70a
Process markdown cloud links for PHP
michaelbausor 834373d
Add comment reformatter to share code
michaelbausor cea4eeb
Add fluent interface
michaelbausor 0c83bf0
Merge branch 'master' into php-markdown-docs
michaelbausor 1b901db
Remove trim
michaelbausor 7dba1e4
Switch to fluent interface only, support python
michaelbausor 8e3d52d
Restructure to CommentTransformer
michaelbausor 191bb30
Revert rename of CommentReformatter
michaelbausor 78f9801
Push functionality into ProtoLinkPattern
michaelbausor 726a2bf
Remove use of matcher in function signatures
michaelbausor 06bceaf
Change model to constructed transformer
michaelbausor 0ca0060
Update handling of link formats
michaelbausor 0a4e52c
Update docs
michaelbausor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
src/main/java/com/google/api/codegen/util/CommentReformatting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* Copyright 2017 Google Inc | ||
* | ||
* 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.google.api.codegen.util; | ||
|
||
import com.google.api.codegen.CommentPatterns; | ||
import com.google.common.base.Function; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** A collection of static helper methods for reformatting comments. */ | ||
public class CommentReformatting { | ||
|
||
public static String CLOUD_URL_PREFIX = "https://cloud.google.com"; | ||
|
||
private CommentReformatting() {} | ||
|
||
public static String reformatAbsoluteMarkdownLinks(String comment, String linkFormat) { | ||
return reformatPattern( | ||
comment, CommentPatterns.ABSOLUTE_LINK_PATTERN, reformatLinkFunction(linkFormat, "")); | ||
} | ||
|
||
public static String reformatCloudMarkdownLinks(String comment, String linkFormat) { | ||
return reformatPattern( | ||
comment, | ||
CommentPatterns.CLOUD_LINK_PATTERN, | ||
reformatLinkFunction(linkFormat, CLOUD_URL_PREFIX)); | ||
} | ||
|
||
public static String reformatPattern( | ||
String comment, Pattern pattern, Function<Matcher, String> replacementFunction) { | ||
StringBuffer sb = new StringBuffer(); | ||
Matcher m = pattern.matcher(comment); | ||
if (!m.find()) { | ||
return comment; | ||
} | ||
do { | ||
m.appendReplacement(sb, replacementFunction.apply(m)); | ||
} while (m.find()); | ||
m.appendTail(sb); | ||
return sb.toString(); | ||
} | ||
|
||
public static Formatter of(String comment) { | ||
return new Formatter(comment); | ||
} | ||
|
||
/** | ||
* The Formatter object allows the static methods in CommentReformatting to be used via a fluent | ||
* interface. | ||
*/ | ||
public static class Formatter { | ||
private String comment; | ||
|
||
private Formatter(String comment) { | ||
this.comment = comment; | ||
} | ||
|
||
public Formatter reformat(Pattern pattern, Function<Matcher, String> replacementFunction) { | ||
comment = CommentReformatting.reformatPattern(comment, pattern, replacementFunction); | ||
return this; | ||
} | ||
|
||
public Formatter reformatAbsoluteMarkdownLinks(String linkFormat) { | ||
comment = CommentReformatting.reformatAbsoluteMarkdownLinks(comment, linkFormat); | ||
return this; | ||
} | ||
|
||
public Formatter reformatCloudMarkdownLinks(String linkFormat) { | ||
comment = CommentReformatting.reformatCloudMarkdownLinks(comment, linkFormat); | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return comment; | ||
} | ||
} | ||
|
||
private static Function<Matcher, String> reformatLinkFunction( | ||
final String linkFormat, final String urlPrefix) { | ||
return new Function<Matcher, String>() { | ||
@Override | ||
public String apply(Matcher matcher) { | ||
String url = urlPrefix + matcher.group(2); | ||
return Matcher.quoteReplacement(String.format(linkFormat, matcher.group(1), url)); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.