Skip to content

Commit 7d85d74

Browse files
committed
Initial commit
Draft commit - whitespace changes still need to be addressed
1 parent 5baae09 commit 7d85d74

File tree

5 files changed

+154
-3
lines changed

5 files changed

+154
-3
lines changed

codegen/aws/core/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ extra["moduleName"] = "software.amazon.smithy.python.aws.codegen"
1212
dependencies {
1313
implementation(project(":core"))
1414
implementation(libs.smithy.aws.traits)
15+
implementation("org.jsoup:jsoup:1.19.1")
1516
}
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package software.amazon.smithy.python.aws.codegen;
6+
7+
import software.amazon.smithy.model.shapes.Shape;
8+
import software.amazon.smithy.model.traits.DocumentationTrait;
9+
import software.amazon.smithy.python.codegen.PythonSettings;
10+
import software.amazon.smithy.python.codegen.integrations.PythonIntegration;
11+
import software.amazon.smithy.utils.SmithyInternalApi;
12+
import software.amazon.smithy.model.Model;
13+
import software.amazon.smithy.model.transform.ModelTransformer;
14+
import org.jsoup.Jsoup;
15+
import org.jsoup.nodes.Document;
16+
import org.jsoup.nodes.Element;
17+
import org.jsoup.nodes.Node;
18+
import org.jsoup.nodes.TextNode;
19+
import org.jsoup.select.NodeVisitor;
20+
21+
/**
22+
* Adds a runtime plugin to set user agent.
23+
*/
24+
@SmithyInternalApi
25+
public class AwsDocConverter implements PythonIntegration {
26+
@Override
27+
public Model preprocessModel(Model model, PythonSettings settings) {
28+
return ModelTransformer.create().mapShapes(model, shape -> {
29+
if (shape.hasTrait(DocumentationTrait.class)) {
30+
DocumentationTrait docTrait = shape.getTrait(DocumentationTrait.class).get();
31+
String html = docTrait.getValue();
32+
String rst = convertHtmlToRst(html);
33+
DocumentationTrait newDocTrait = new DocumentationTrait(rst);
34+
return Shape.shapeToBuilder(shape)
35+
.addTrait(newDocTrait)
36+
.build();
37+
} else {
38+
return shape;
39+
}
40+
});
41+
}
42+
43+
private String convertHtmlToRst(String html) {
44+
Document document = Jsoup.parse(html);
45+
RstNodeVisitor visitor = new RstNodeVisitor();
46+
document.body().traverse(visitor);
47+
return visitor.toString();
48+
}
49+
50+
private static class RstNodeVisitor implements NodeVisitor {
51+
private final StringBuilder sb = new StringBuilder();
52+
private boolean inList = false;
53+
54+
@Override
55+
public void head(Node node, int depth) {
56+
if (node instanceof TextNode) {
57+
//TODO properly handle stripping whitespace
58+
Node parentNode = node.parent();
59+
if (parentNode != null && parentNode.nodeName().equals("p")) {
60+
//TODO write a test case like the following: <p> Foo <i>bar
61+
// </i> baz</p> -> "Foo *bar* baz"
62+
sb.append(((TextNode) node).text().strip());
63+
} else {
64+
sb.append(((TextNode) node).text());
65+
}
66+
} else if (node instanceof Element) {
67+
Element element = (Element) node;
68+
switch (element.tagName()) {
69+
case "a":
70+
sb.append("`");
71+
break;
72+
case "b":
73+
case "strong":
74+
sb.append("**");
75+
break;
76+
case "i":
77+
case "em":
78+
sb.append("*");
79+
break;
80+
case "code":
81+
sb.append(" ``");
82+
break;
83+
case "important":
84+
sb.append(".. important::\n\n ");
85+
break;
86+
case "note":
87+
sb.append(".. note::\n\n ");
88+
break;
89+
//TODO this looks a little weird on modelid for invoke_model input
90+
// do I do something weird based on if it's in a parameter cause
91+
// those are already bullets?
92+
case "ul":
93+
inList = true;
94+
sb.append("\n");
95+
break;
96+
case "li":
97+
if (inList) {
98+
sb.append("- ");
99+
}
100+
break;
101+
}
102+
}
103+
}
104+
105+
@Override
106+
public void tail(Node node, int depth) {
107+
if (node instanceof Element) {
108+
Element element = (Element) node;
109+
switch (element.tagName()) {
110+
case "a":
111+
sb.append(" <").append(element.attr("href")).append(">`_ ");
112+
break;
113+
case "b":
114+
case "strong":
115+
sb.append("**");
116+
break;
117+
case "i":
118+
case "em":
119+
sb.append("*");
120+
break;
121+
case "code":
122+
sb.append("`` ");
123+
break;
124+
case "important":
125+
case "note":
126+
sb.append("\n\n");
127+
break;
128+
case "ul":
129+
inList = false;
130+
sb.append("\n");
131+
break;
132+
case "p":
133+
sb.append("\n\n");
134+
break;
135+
}
136+
}
137+
}
138+
139+
@Override
140+
public String toString() {
141+
return sb.toString().trim();
142+
}
143+
}
144+
}

codegen/core/src/main/java/software/amazon/smithy/python/codegen/ClientGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ private void generateService(PythonWriter writer) {
6969
$L
7070
7171
:param config: Optional configuration for the client. Here you can set things like the
72-
endpoint for HTTP services or auth credentials.
72+
endpoint for HTTP services or auth credentials.
7373
7474
:param plugins: A list of callables that modify the configuration dynamically. These
75-
can be used to set defaults, for example.""", docs);
75+
can be used to set defaults, for example.""", docs);
7676
});
7777

7878
var defaultPlugins = new LinkedHashSet<SymbolReference>();

codegen/core/src/main/java/software/amazon/smithy/python/codegen/generators/StructureGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ private boolean hasDocs() {
309309
private void writeMemberDocs(MemberShape member) {
310310
member.getMemberTrait(model, DocumentationTrait.class).ifPresent(trait -> {
311311
String memberName = symbolProvider.toMemberName(member);
312-
String docs = writer.formatDocs(String.format(":param %s: %s", memberName, trait.getValue()));
312+
String docs = writer.formatDocs(String.format(":param %s: %s", memberName, trait.getValue())).replace("\n", "\n\t");
313313
writer.write(docs);
314314
});
315315
}

codegen/core/src/main/java/software/amazon/smithy/python/codegen/integrations/PythonIntegration.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Collections;
88
import java.util.List;
99
import software.amazon.smithy.codegen.core.SmithyIntegration;
10+
import software.amazon.smithy.model.Model;
1011
import software.amazon.smithy.python.codegen.GenerationContext;
1112
import software.amazon.smithy.python.codegen.PythonSettings;
1213
import software.amazon.smithy.python.codegen.generators.ProtocolGenerator;
@@ -39,6 +40,11 @@ default List<RuntimeClientPlugin> getClientPlugins(GenerationContext context) {
3940
return Collections.emptyList();
4041
}
4142

43+
default Model preprocessModel(Model model, PythonSettings settings) {
44+
return model;
45+
}
46+
47+
4248
/**
4349
* Writes out all extra files required by runtime plugins.
4450
*/

0 commit comments

Comments
 (0)