Skip to content

Commit 4fc5c1c

Browse files
Merge pull request #64 from awslabs/crosslink-redirects
Added redirection js and a generator script to be run at doc gen time.
2 parents 9cf190b + 23a7564 commit 4fc5c1c

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<DOCTYPE HTML>
2+
<html lang="en-US">
3+
<head>
4+
<meta charset="UTF-8">
5+
<script type="text/javascript">
6+
7+
var uidServiceData = '${UID_SERVICE_MAPPING}';
8+
9+
var uidToServiceName = JSON.parse(uidServiceData);
10+
11+
var baseUri = "LATEST/";
12+
13+
function getQueryStringParameter(name, url) {
14+
15+
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
16+
name = name.replace(/[\[\]]/g, "\\$&");
17+
18+
results = regex.exec(url);
19+
if (!results) return null;
20+
if (!results[2]) return '';
21+
return decodeURIComponent(results[2].replace(/\+/g, " "));
22+
}
23+
24+
function escapeCharacters(toEscape) {
25+
var escapedString = "";
26+
27+
for(var i = 0; i < toEscape.length; ++i) {
28+
var character = toEscape.charAt(i);
29+
if(character != character.toLowerCase()) {
30+
escapedString += '_' + character.toLowerCase();
31+
}
32+
else if(character == ':') {
33+
escapadString += "_1";
34+
}
35+
else {
36+
escapedString += character;
37+
}
38+
}
39+
40+
return escapedString;
41+
}
42+
43+
function transformRequestToDoxygenLink(url, typeName, operation) {
44+
var uid = getQueryStringParameter("uid", url);
45+
46+
var serviceName = uidToServiceName[uid];
47+
48+
if(serviceName == null) {
49+
return baseUri;
50+
}
51+
52+
var dirtyName = "class_aws_1_1_";
53+
dirtyName += serviceName.charAt(0).toLowerCase() + serviceName.substring(1);
54+
55+
if(typeName != null) {
56+
dirtyName += "_1_1_model_1_1_";
57+
var firstCharLowered = typeName.charAt(0).toLowerCase() + typeName.substring(1);
58+
var operationCanonicalizedStr = firstCharLowered.replace("Input", "Request").replace("Output", "Result");
59+
60+
dirtyName += operationCanonicalizedStr;
61+
}
62+
else if(operation != null) {
63+
dirtyName += "_1_1_";
64+
var firstCharLowered = serviceName.charAt(0).toLowerCase() + serviceName.substring(1);
65+
dirtyName += firstCharLowered;
66+
dirtyName += "_client";
67+
//once we figure out the operation link, add the #stuff here:
68+
//dirtyName += "#operationName";
69+
}
70+
71+
dirtyName += ".html";
72+
return baseUri + escapeCharacters(dirtyName);
73+
}
74+
75+
function checkResourceExists(url) {
76+
var xmlHttp = new XMLHttpRequest();
77+
xmlHttp.open( "GET", url, false );
78+
xmlHttp.send( null );
79+
return xmlHttp.status == 200;
80+
}
81+
82+
var url = window.location.href;
83+
var typeName = getQueryStringParameter("type", url);
84+
var operation = getQueryStringParameter("operation", url);
85+
var firstAttempt = transformRequestToDoxygenLink(url, typeName, operation);
86+
87+
//if the first one exists, load that one.
88+
if(checkResourceExists(firstAttempt)){
89+
window.location.href = firstAttempt;
90+
}
91+
//otherwise, try and swap operation and typename.
92+
else {
93+
var secondAttempt = transformRequestToDoxygenLink(url, operation, typeName)
94+
//if that fails, fall back to the default page.
95+
if(checkResourceExists(secondAttempt)){
96+
window.location.href = secondAttempt;
97+
}
98+
else {
99+
window.location.href = baseUri + "index.html";
100+
}
101+
}
102+
103+
</script>
104+
</head>
105+
<body>
106+
</body>
107+
</html>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import os
2+
import argparse
3+
import io
4+
import codecs
5+
import json
6+
import re
7+
from os import listdir
8+
from os.path import isfile, join
9+
10+
def generateDocsMap(apiDefinitionsPath):
11+
sdks = {}
12+
13+
filesInDir = [f for f in listdir(apiDefinitionsPath) if isfile(join(apiDefinitionsPath, f))]
14+
15+
for file in filesInDir:
16+
match = re.search('([\w\d-]+)-(\d{4}-\d{2}-\d{2}).normal.json', file)
17+
if match:
18+
with codecs.open(join(apiDefinitionsPath, file), 'rb', 'utf-8') as api_definition:
19+
api_content = json.loads(api_definition.read())
20+
if "uid" in api_content["metadata"].keys():
21+
sdks[api_content["metadata"]["uid"]] = getServiceNameFromMetadata(api_content["metadata"])
22+
23+
return sdks
24+
25+
def getServiceNameFromMetadata(metadataNode):
26+
toSanitize = ""
27+
if "serviceAbbreviation" in metadataNode.keys():
28+
toSanitize = metadataNode["serviceAbbreviation"]
29+
else:
30+
toSanitize = metadataNode["serviceFullName"]
31+
32+
return toSanitize.replace(" ","").replace("-", "").replace("_", "").replace("Amazon", "").replace("AWS", "").replace("/", "")
33+
34+
def insertDocsMapToRedirect(apiDefinitionsPath, templatePath, outputPath):
35+
sdks = generateDocsMap(apiDefinitionsPath)
36+
37+
output = ""
38+
with codecs.open(templatePath, 'rb', 'utf-8') as redirect_template:
39+
current_template = redirect_template.read();
40+
output = current_template.replace("${UID_SERVICE_MAPPING}", json.dumps(sdks, ensure_ascii=False))
41+
with open(outputPath, 'w') as redirect_output:
42+
redirect_output.write(output)
43+
44+
def Main():
45+
parser = argparse.ArgumentParser(description="Generates a Cross-link redirect file.")
46+
parser.add_argument("--apiDefinitionsPath", action="store")
47+
parser.add_argument("--templatePath", action="store")
48+
parser.add_argument("--outputPath", action="store")
49+
50+
args = vars( parser.parse_args() )
51+
argMap = {}
52+
argMap[ "apiDefinitionsPath" ] = args[ "apiDefinitionsPath" ] or "../code-generation/api-descriptions"
53+
argMap[ "templatePath" ] = args[ "templatePath" ] or "./"
54+
argMap[ "outputPath" ] = args[ "outputPath" ] or "/"
55+
56+
insertDocsMapToRedirect(argMap["apiDefinitionsPath"], argMap["templatePath"], argMap["outputPath"])
57+
58+
Main()
59+

0 commit comments

Comments
 (0)