forked from gooddata/gooddata-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseUrlBreadcrumbs.html
More file actions
116 lines (105 loc) · 4.07 KB
/
parseUrlBreadcrumbs.html
File metadata and controls
116 lines (105 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
{{- $url := . -}}
{{- $variant := "full" -}}
{{- /* Check if variant is passed as context (dict) or just the URL string */ -}}
{{- if reflect.IsMap . -}}
{{- $url = .url -}}
{{- $variant = .variant | default "full" -}}
{{- end -}}
{{- $urlObj := urls.Parse $url -}}
{{- $host := printf "%s://%s" $urlObj.Scheme $urlObj.Host -}}
{{- $pathname := $urlObj.Path -}}
{{- $breadcrumbs := slice -}}
{{- $segments := split $pathname "/" -}}
{{- $currentPath := "" -}}
{{- if eq $variant "before-version" -}}
{{/* Variant 1: Return only the base path up to and including /latest/ or any version from Site.Params.versions */}}
{{- $versionFound := false -}}
{{- $versionIndex := 0 -}}
{{- range $index, $segment := $segments -}}
{{- if ne $segment "" -}}
{{/* Check if segment is "latest" */}}
{{- if eq $segment "latest" -}}
{{- $versionFound = true -}}
{{- $versionIndex = $index -}}
{{- else -}}
{{/* Check if segment matches any version in Site.Params.versions or is a semantic version */}}
{{- if and ($.Site.Params) ($.Site.Params.versions) -}}
{{- range $.Site.Params.versions -}}
{{- if eq $segment .version -}}
{{- $versionFound = true -}}
{{- $versionIndex = $index -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{/* Fallback: check if segment looks like a version (1.51, 1.52, etc.) */}}
{{- if or (eq $segment "dev") (eq $segment "master") (findRE `^\d+\.\d+` $segment) -}}
{{- $versionFound = true -}}
{{- $versionIndex = $index -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if $versionFound -}}
{{- $currentPath = "" -}}
{{- range $index, $segment := $segments -}}
{{- if and (le $index $versionIndex) (ne $segment "") -}}
{{- $currentPath = printf "%s%s/" $currentPath $segment -}}
{{- end -}}
{{- end -}}
{{- $fullUrl := printf "%s/%s" $host $currentPath -}}
{{- $breadcrumbs = $breadcrumbs | append $fullUrl -}}
{{- end -}}
{{- else if eq $variant "after-version" -}}
{{/* Variant 2: Start parsing from /latest/ or any version from Site.Params.versions */}}
{{- $versionFound := false -}}
{{- $startIndex := 0 -}}
{{- range $index, $segment := $segments -}}
{{- if ne $segment "" -}}
{{/* Check if segment is "latest" */}}
{{- if eq $segment "latest" -}}
{{- $versionFound = true -}}
{{- $startIndex = $index -}}
{{- else -}}
{{/* Check if segment matches any version in Site.Params.versions or is a semantic version */}}
{{- if and ($.Site.Params) ($.Site.Params.versions) -}}
{{- range $.Site.Params.versions -}}
{{- if eq $segment .version -}}
{{- $versionFound = true -}}
{{- $startIndex = $index -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{/* Fallback: check if segment looks like a version (1.51, 1.52, etc.) */}}
{{- if or (eq $segment "dev") (eq $segment "master") (findRE `^\d+\.\d+` $segment) -}}
{{- $versionFound = true -}}
{{- $startIndex = $index -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if $versionFound -}}
{{- $currentPath = "" -}}
{{- range $index, $segment := $segments -}}
{{- if and (ge $index $startIndex) (ne $segment "") -}}
{{- $currentPath = printf "%s%s/" $currentPath $segment -}}
{{- $fullUrl := printf "%s/%s" $host $currentPath -}}
{{- $breadcrumbs = $breadcrumbs | append $fullUrl -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{/* Default variant: Parse all segments */}}
{{- range $segments -}}
{{- if ne . "" -}}
{{- $currentPath = printf "%s%s/" $currentPath . -}}
{{- $fullUrl := printf "%s/%s" $host $currentPath -}}
{{- $breadcrumbs = $breadcrumbs | append $fullUrl -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- if gt (len $breadcrumbs) 0 -}}
{{- $breadcrumbs | jsonify }}
{{- else -}}
[]{{- end -}}