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 9 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
69 changes: 69 additions & 0 deletions
69
src/main/java/com/google/api/codegen/util/CommentTransformer.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,69 @@ | ||
/* 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.common.base.Function; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class CommentTransformer { | ||
|
||
public static String CLOUD_URL_PREFIX = "https://cloud.google.com"; | ||
|
||
private String comment; | ||
|
||
private CommentTransformer(String comment) { | ||
this.comment = comment; | ||
} | ||
|
||
public static CommentTransformer of(String comment) { | ||
return new CommentTransformer(comment); | ||
} | ||
|
||
public CommentTransformer replace(Pattern pattern, String replacement) { | ||
comment = pattern.matcher(comment).replaceAll(replacement); | ||
return this; | ||
} | ||
|
||
public CommentTransformer transform(Transformation transformation) { | ||
comment = transformation.apply(comment); | ||
return this; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return comment; | ||
} | ||
|
||
public static class Transformation { | ||
private Pattern pattern; | ||
private Function<Matcher, String> replacementFunction; | ||
|
||
public Transformation(Pattern pattern, Function<Matcher, String> replacementFunction) { | ||
this.pattern = pattern; | ||
this.replacementFunction = replacementFunction; | ||
} | ||
|
||
public String apply(String comment) { | ||
StringBuffer sb = new StringBuffer(); | ||
Matcher m = pattern.matcher(comment); | ||
while (m.find()) { | ||
m.appendReplacement(sb, replacementFunction.apply(m)); | ||
} | ||
m.appendTail(sb); | ||
return sb.toString(); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/google/api/codegen/util/ProtoLinkPattern.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,53 @@ | ||
/* 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.api.codegen.util.CommentTransformer.Transformation; | ||
import com.google.common.base.Function; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* An enumeration of link formats found in proto files. Each element of the enumeration is | ||
* associated with a {@link Pattern} object that must contain exactly two groups matching the link | ||
* title and link url. | ||
*/ | ||
public enum ProtoLinkPattern { | ||
ABSOLUTE(CommentPatterns.ABSOLUTE_LINK_PATTERN, ""), | ||
CLOUD(CommentPatterns.CLOUD_LINK_PATTERN, CommentTransformer.CLOUD_URL_PREFIX), | ||
PROTO(CommentPatterns.PROTO_LINK_PATTERN, ""); | ||
|
||
private Pattern pattern; | ||
private String urlPrefix; | ||
|
||
ProtoLinkPattern(Pattern pattern, String urlPrefix) { | ||
this.pattern = pattern; | ||
this.urlPrefix = urlPrefix; | ||
} | ||
|
||
public Transformation createTransformation(final String linkFormat) { | ||
return new Transformation( | ||
pattern, | ||
new Function<Matcher, String>() { | ||
@Override | ||
public String apply(Matcher matcher) { | ||
String title = matcher.group(1); | ||
String url = urlPrefix + matcher.group(2); | ||
return Matcher.quoteReplacement(String.format(linkFormat, title, 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
Oops, something went wrong.
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.