Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(html): Optionally create hyperlinks of URLs in feature properties #229

Merged
merged 1 commit into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ type WebConfig struct {
// Viewer config for displaying a single feature on a map
// +optional
FeatureViewer *FeaturesViewer `yaml:"featureViewer,omitempty" json:"featureViewer,omitempty"`

// Whether URLs (to external resources) in the HTML representation of features should be rendered as hyperlinks.
// +optional
URLAsHyperlink bool `yaml:"urlAsHyperlink,omitempty" json:"urlAsHyperlink,omitempty"`
}

// +kubebuilder:object:generate=true
Expand Down
5 changes: 4 additions & 1 deletion examples/config_all.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ metadata: &addressMetadata
- 7.37403
storageCrs: http://www.opengis.net/def/crs/OGC/1.3/CRS84


# main config
version: 1.0.0
title: Demo of all OGC specs in one API
# shortened title, used in breadcrumb path
Expand Down Expand Up @@ -96,6 +96,8 @@ ogcApi:
collections:
- id: addresses # same collection as the geovolumes/tiles
metadata: *addressMetadata
web:
urlAsHyperlink: true
- id: addresses2
tableName: addresses

Expand All @@ -106,6 +108,7 @@ ogcApi:
metadata: *addressMetadata
tileServerPath: "NewYork/3DTiles"
uriTemplate3dTiles: "3DTiles/{level}/{x}/{y}.b3m"

styles:
default: dummy-style
stylesDir: ./examples/resources
Expand Down
11 changes: 11 additions & 0 deletions internal/engine/templatefuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package engine
import (
htmltemplate "html/template"
"log"
"regexp"
"strconv"
"strings"
texttemplate "text/template"
Expand All @@ -19,6 +20,7 @@ import (

var (
globalTemplateFuncs texttemplate.FuncMap
linkRegex = regexp.MustCompile(`https?://\S+`)
)

// Initialize functions to be used in html/json/etc templates
Expand All @@ -30,6 +32,7 @@ func init() {
"humansize": humanSize,
"bytessize": bytesSize,
"isdate": isDate,
"islink": isLink,
}
sprigFuncs := sprig.FuncMap() // we also support https://github.com/go-task/slim-sprig functions
globalTemplateFuncs = combineFuncMaps(customFuncs, sprigFuncs)
Expand Down Expand Up @@ -111,3 +114,11 @@ func isDate(v any) bool {
}
return false
}

// isLink true when given input is an HTTP(s) URL, false otherwise
func isLink(v any) bool {
if text, ok := v.(string); ok {
return linkRegex.MatchString(text)
}
return false
}
30 changes: 29 additions & 1 deletion internal/ogc/features/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ func TestFeatures(t *testing.T) {
},
},
{
name: "Request features for collection with specific web/viewer configuration, to make sure this is reflected in the HTML output",
name: "Request features for collection with specific viewer configuration, to make sure this is reflected in the HTML output",
fields: fields{
configFile: "internal/ogc/features/testdata/config_features_webconfig.yaml",
url: "http://localhost:8080/collections/:collectionId/items?f=html",
Expand All @@ -573,6 +573,20 @@ func TestFeatures(t *testing.T) {
statusCode: http.StatusOK,
},
},
{
name: "Request features for collection with specific web configuration, and make sure URLs are rendered as hyperlinks in HTML output",
fields: fields{
configFile: "internal/ogc/features/testdata/config_features_webconfig.yaml",
url: "http://localhost:8080/collections/:collectionId/items?f=html",
collectionID: "ligplaatsen",
contentCrs: "<" + domain.WGS84CrsURI + ">",
format: "html",
},
want: want{
body: "internal/ogc/features/testdata/expected_webconfig_hyperlink_snippet.html",
statusCode: http.StatusOK,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -925,6 +939,20 @@ func TestFeatures_Feature(t *testing.T) {
statusCode: http.StatusOK,
},
},
{
name: "Request feature with specific web configuration, and make sure URLs are rendered as hyperlinks in HTML output",
fields: fields{
configFile: "internal/ogc/features/testdata/config_features_webconfig.yaml",
url: "http://localhost:8080/collections/:collectionId/items/:featureId?f=html",
collectionID: "ligplaatsen",
featureID: "4030",
format: "html",
},
want: want{
body: "internal/ogc/features/testdata/expected_webconfig_hyperlink_snippet.html",
statusCode: http.StatusOK,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
20 changes: 14 additions & 6 deletions internal/ogc/features/templates/feature.go.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,26 @@ <h1 class="title h2" id="title">{{ .Config.Title }} - {{ if and .Params.Metadata
{{ $skipKeys = append $skipKeys $mapSheetProperties.Size }}
{{ end }}
{{ range $key, $value := .Params.Properties }}
{{ if not (has $key $skipKeys) }}
{{- if not (has $key $skipKeys) -}}
<tr>
<td class="w-25">{{ $key }}</td>
{{ if isdate $value }}

{{- if isdate $value -}}
<td>{{ $value | date "2006/01/02 15:04:05" }}</td>
{{ else if hasSuffix ".href" $key }}

{{- else if hasSuffix ".href" $key -}}
{{- /* render links for relations between features (OGC API part 5) */ -}}
<td><a href="{{ $value }}" target="_blank">{{ $value }}</a></td>

{{- else if and $webConfig $webConfig.URLAsHyperlink (islink $value) -}}
{{- /* render links to arbitray (external) URLs */ -}}
<td><a href="{{ $value }}" target="_blank">{{ $value }}</a></td>
{{ else }}

{{- else -}}
<td>{{ $value }}</td>
{{ end }}
{{- end -}}
</tr>
{{ end }}
{{- end -}}
{{ end }}
{{/* for map sheets collection, add download button */}}
{{ if $mapSheetProperties }}
Expand Down
20 changes: 14 additions & 6 deletions internal/ogc/features/templates/features.go.html
Original file line number Diff line number Diff line change
Expand Up @@ -277,18 +277,26 @@ <h2 class="card-header h5">
{{ $skipKeys = append $skipKeys $mapSheetProperties.Size }}
{{ end }}
{{ range $key, $value := $feat.Properties }}
{{ if not (has $key $skipKeys) }}
{{- if not (has $key $skipKeys) -}}
<tr>
<td class="w-20">{{ $key }}</td>
{{ if isdate $value }}

{{- if isdate $value -}}
<td>{{ $value | date "2006/01/02 15:04:05" }}</td>
{{ else if hasSuffix ".href" $key }}

{{- else if hasSuffix ".href" $key -}}
{{- /* render links for relations between features (OGC API part 5) */ -}}
<td><a href="{{ $value }}" target="_blank">{{ $value }}</a></td>
{{ else }}

{{- else if and $webConfig $webConfig.URLAsHyperlink (islink $value) -}}
{{- /* render links to arbitray (external) URLs */ -}}
<td><a href="{{ $value }}" target="_blank">{{ $value }}</a></td>

{{- else -}}
<td>{{ $value }}</td>
{{ end }}
{{- end -}}
</tr>
{{ end }}
{{- end -}}
{{ end }}
{{/* for map sheets collection, add download button */}}
{{ if $mapSheetProperties }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ ogcApi:
maxScale: 40000
featureViewer:
minScale: 22
urlAsHyperlink: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<td><a href="http://bag.basisregistraties.overheid.nl/bag/id/nummeraanduiding/0363200000428648" target="_blank">http://bag.basisregistraties.overheid.nl/bag/id/nummeraanduiding/0363200000428648</a></td>

Loading