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

Adds client examples #86

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public/**
resources/**
tmp/**
content/**
examples/**

# Local Netlify folder
.netlify
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ LOGLEVEL ?= INFO
ENV ?= development
HUGO_CONTENT ?= ./content/en
HUGO_BUILD ?= --gc
HUGO_SERVER ?= --quiet --disableFastRender -b http://$(IP) --bind $(IP)
HUGO_SERVER ?= --disableFastRender -b http://$(IP) --bind $(IP)
DOCKER_IMAGE=image-redis-stack-website
DOCKER_CONTAINER=container-$(DOCKER_IMAGE)
DOCKER_PORT=-p 1313:1313
Expand Down Expand Up @@ -41,7 +41,7 @@ up:

clean:
# @rm -f data/*.json
@rm -rf $(HUGO_CONTENT) public tmp
@rm -rf $(HUGO_CONTENT) examples public tmp

ifneq ($(VOL),)
DOCKER_VOL=-v $(VOL):$(VOL)
Expand Down
51 changes: 45 additions & 6 deletions assets/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,39 @@ select {
line-height: 1.25rem;
}

.codetabs > .panel .highlight .chroma .lntd > pre {
@apply p-0;
.codetabs > .panel .highlight .chroma > .lntable td:first-child .chroma {
@apply rounded-none p-0;
}

.codetabs > .panel .highlight .chroma > .lntable td:last-child .chroma {
@apply rounded-none p-0;
}

.codetabs > .panel:not([aria-expanded]) .highlight .chroma > code > .line:not(.hl),
.codetabs > .panel:not([aria-expanded]) .highlight .chroma > .lntable .lntd code > .lnt,
.codetabs > .panel:not([aria-expanded]) .highlight .chroma > .lntable .lntd code > .line:not(.hl) {
@apply hidden;
}

.codetabs > .panel:where([aria-expanded]) .highlight .chroma > code > .line:not(.hl),
.codetabs > .panel:where([aria-expanded]) .highlight .chroma > .lntable .lntd code > .lnt,
.codetabs > .panel:where([aria-expanded]) .highlight .chroma > .lntable .lntd code > .line:not(.hl) {
@apply opacity-60;
}

.codetabs > .panel .highlight .chroma > code > .hl,
.codetabs > .panel .highlight .chroma > .lntable .lntd code > .hl {
@apply bg-inherit;
}

.codetabs > .panel:where([aria-expanded]) #visibility-on,
.codetabs > .panel:not([aria-expanded]) #visibility-off {
@apply hidden;
}

.codetabs > .panel:where([aria-expanded]) #visibility-off,
.codetabs > .panel:not([aria-expanded]) #visibility-on {
@apply block;
}

.download-cards {
Expand Down Expand Up @@ -558,24 +589,32 @@ html {
@apply align-top p-0 m-0 border-0;
}

.chroma .lntd:last-child {
@apply flex-1;
}

/* LineTable */
.chroma .lntable {
@apply p-0 m-0 border-0 w-auto;
@apply p-0 m-0 border-0 min-w-full;
}

/* LineHighlight */
.chroma .hl {
@apply bg-yellow-100;
@apply flex bg-indigo-900/80;
}

/* LineNumbersTable */
.chroma .lnt {
@apply text-slate-400 whitespace-pre mr-4 px-2;
@apply text-slate-400 whitespace-pre pl-2 pr-4;
}

/* LineNumbers */
.chroma .ln {
@apply text-slate-400 whitespace-pre mr-4 px-4;
@apply text-slate-400 whitespace-pre px-4;
}

.chroma .lntable tr {
@apply flex items-stretch;
}

/* Line */
Expand Down
60 changes: 52 additions & 8 deletions build/stack/component.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from ast import dump
from email.mime import base
import logging
import os
import semver
Expand Down Expand Up @@ -208,8 +206,8 @@ def _preview_mode(self) -> bool:

def _skip_checkout(self, obj) -> bool:
if obj.get('git_uri') == self._repo_uri() and self._preview_mode():
return False
return True
return True
return False

def _checkout(self, ref, dest, obj):
if not self._skip_checkout(obj):
Expand All @@ -227,6 +225,7 @@ def __init__(self, filepath: str, root: dict = None, args: dict = None):
self._website = self.get('website')
self._skip_clone = self.get('skip_clone')
self._content = f'{self._website.get("path")}/{self._website.get("content")}'
self._examples = {}
mkdir_p(self._content)

def _persist_commands(self) -> None:
Expand All @@ -239,6 +238,11 @@ def _persist_groups(self) -> None:
logging.info(f'Persisting {self._id} groups: {filepath}')
dump_dict(filepath, self._groups)

def _persist_examples(self) -> None:
filepath = f'{self._website.get("path")}/{self._website.get("examples")}'
logging.info(f'Persisting {self._id} examples: {filepath}')
dump_dict(filepath, self._examples)

def _persist_versions(self) -> None:
filepath = f'{self._website.get("path")}/{self._website.get("versions")}'
logging.info(f'Persisting {self._id} versions: {filepath}')
Expand Down Expand Up @@ -296,7 +300,7 @@ def _process_docs(self) -> None:
md.process_doc(self._commands)

def apply(self) -> None:
for kind in ['core', 'docs', 'modules', 'assets']:
for kind in ['core', 'docs', 'modules', 'clients', 'assets']:
for component in self.get(kind):
if type(component) == str:
basename, ext = os.path.splitext(component)
Expand All @@ -312,13 +316,16 @@ def apply(self) -> None:
c = Module(filename, self)
else:
continue
elif kind == 'clients':
c = Client(filename, self)
elif kind == 'assets':
c = Asset(filename, self)
else:
die(f'Unknown component definition for {component}')
c.apply()
self._persist_commands()
self._persist_groups()
self._persist_examples()
self._persist_versions()
self._process_commands()
self._process_docs()
Expand Down Expand Up @@ -455,16 +462,53 @@ def apply(self) -> None:
self._get_groups()
return files

class Client(Component):
def __init__(self, filepath: str, root: dict = None):
super().__init__(filepath, root)

def _copy_examples(self):
if ex := self.get('examples'):
repo = self._git_clone(ex)
dev_branch = ex.get('dev_branch')
self._checkout(dev_branch, repo, ex)
path = ex.get('path', '')
logging.info(f'Copying {self._id} examples')
src = f'{repo}/{path}/'
dst = f'{self._root._website.get("path")}/{self._root._website.get("examples_path")}'

_, dirs, _ = next(os.walk(src))
for d in dirs:
meta = f'{src}/{d}/example.json'
try:
ex = load_dict(meta)
exid = ex.pop('id')
except FileNotFoundError:
logging.warn(f'Example "{meta}" not found for {self._id}: skipping.')
continue
ex['source'] = f'{src}/{d}/{ex.get("file")}'
if not os.path.isfile(ex['source']):
logging.warn(f'"Source {ex.get("source")}" not found for {self._id}: skipping.')
continue

mkdir_p(f'{dst}/{exid}')
rsync(ex['source'],f'{dst}/{exid}/')
ex['target'] = f'{dst}/{exid}/{ex.get("file")}'
examples = self._root._examples
if not examples.get(exid):
examples[exid] = {}
examples[exid][self.get('language')] = ex

def apply(self) -> None:
logging.info(f'Applying client {self._id}')
self._copy_examples()

class Asset(Component):
def __init__(self, filepath: str, root: dict = None):
super().__init__(filepath, root)

def apply(self) -> None:
logging.info(f'Applying asset {self._id}')
if not self._repository:
return
repo = self._git_clone(self._repository)
dev_branch = self._repository.get('dev_branch')
self._checkout(dev_branch, repo, self._repository)
self._checkout(dev_branch, repo, self._repository) #
return Component._dump_payload(repo, './', self._repository.get('payload'))
60 changes: 60 additions & 0 deletions build/stack/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import logging
import re

START_ANCHOR = 'HIDE_START'
END_ANCHOR = 'HIDE_END'
PREFIXES = {
'python': '#',
'javascript': '//',
}

class Example():
language = None
path = None
content = None
hidden = []

def __init__(self, language: str, path: str) -> None:
if not PREFIXES.get(language):
logging.error(f'Unknown language "{language}" for example {path}')
return
self.language = language
self.path = path
with open(path, 'r') as f:
self.content = f.readlines()

def persist(self, path: str = None) -> None:
if not path:
path = self.path
with open(path,'w') as f:
f.writelines(self.content)

def make_ranges(self) -> None:
curr = 0
hidden = None
content = []
start = re.compile(f'^{PREFIXES[self.language]}\\s?{START_ANCHOR}')
end = re.compile(f'^{PREFIXES[self.language]}\\s?{END_ANCHOR}')
while curr < len(self.content):
l = self.content[curr]
if re.search(start, l):
if hidden is not None:
logging.error(f'Nested hidden anchor in {self.path}:L{curr+1} - aborting.')
return
hidden = len(content)
elif re.search(end, l):
if hidden is None:
logging.error(f'Closing hidden anchor w/o a start in {self.path}:L{curr+1} - aborting.')
return
if len(content) - hidden == 1:
self.hidden.append(f'{hidden+1}')
else:
self.hidden.append(f'{hidden+1}-{len(content)}')
hidden = None
else:
content.append(l)
curr += 1
if hidden is not None:
logging.error(f'Unclosed hidden anchor in {self.path}:L{hidden+1} - aborting.')
return
self.content = content
5 changes: 3 additions & 2 deletions build/stack/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ def die(msg: str = 'aborting - have a nice day!') -> None:
exit(1)


def rsync(src: str, dst: str, exclude: list = ['.*']):
def rsync(src: str, dst: str, exclude: list = ['.*'], include: list = ['*']):
ex = [f'"{x}"' for x in exclude]
cmd = f'rsync -av --no-owner --no-group --exclude={{{",".join(ex)}}} {src} {dst}'
ix = [f'"{x}"' for x in include]
cmd = f'rsync -av --no-owner --no-group --include={{{",".join(ix)}}} --exclude={{{",".join(ex)}}} {src} {dst}'
ret = run(cmd)
return ret.split('\n')

Expand Down
2 changes: 2 additions & 0 deletions config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ anchor = "smart"

[params]
tagManagerId = "GTM-T4MTBKP"
# Display and sort order for client examples
clientsExamples = ["Python", "JavaScript"]

# Language configuration

Expand Down
6 changes: 6 additions & 0 deletions data/stack/index.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"docs": [
"stack_docs"
],
"clients": [
"node_redis",
"redis_py"
],
"modules": [
"redisearch",
"redisjson",
Expand All @@ -24,6 +28,8 @@
"website": {
"path": "./",
"content": "content/en",
"examples": "data/examples.json",
"examples_path": "examples",
"commands": "data/commands.json",
"groups": "data/groups.json",
"versions": "data/versions.json",
Expand Down
14 changes: 14 additions & 0 deletions data/stack/node_redis.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "node_redis",
"type": "client",
"name": "node-redis",
"language": "JavaScript",
"repository": {
"git_uri": "https://github.com/redis/node-redis"
},
"examples": {
"git_uri": "https://github.com/itamarhaber/node-redis",
"dev_branch": "emb-examples",
"path": "examples"
}
}
14 changes: 14 additions & 0 deletions data/stack/redis_py.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "redis_py",
"type": "client",
"name": "redis-py",
"language": "Python",
"repository": {
"git_uri": "https://github.com/redis/redis-py"
},
"examples": {
"git_uri": "https://github.com/itamarhaber/redis-py",
"dev_branch": "emb-examples",
"path": "docs/examples"
}
}
4 changes: 2 additions & 2 deletions data/stack/stack_docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"description": "Redis Stack docs",
"stack_path": "docs",
"docs": {
"git_uri": "https://github.com/redis-stack/redis-stack-docs",
"dev_branch": "main",
"git_uri": "https://github.com/itamarhaber/redis-stack-docs",
"dev_branch": "emb-examples",
"path": "docs"
},
"misc": {
Expand Down
21 changes: 21 additions & 0 deletions layouts/partials/tabbed-clients-example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{{ $id := .Scratch.Get "example" }}
{{ if not (isset $.Site.Data.examples $id) }}
{{ warnf "[tabbed-clients-example] Example not found %q for %q" $id $.Page }}
{{ end }}

{{ $tabs := slice }}
{{ $languages := index $.Site.Data.examples $id }}
{{ range $language := $.Site.Params.clientsexamples }}
{{ $example := index $languages $language }}
{{ $examplePath := index $example "target" }}
{{ $options := printf "linenos=false" }}
{{ if isset $example "highlight" }}
{{ $options = printf "%s,hl_lines=%s" $options (delimit (index $example "highlight") " ") }}
{{ end }}
{{ $params := dict "language" $language "contentPath" $examplePath "options" $options }}
{{ $content := partial "tabs/source.html" $params }}
{{ $tabs = $tabs | append (dict "title" $language "content" $content) }}
{{ end }}

{{ $params := dict "id" $id "tabs" $tabs }}
{{ partial "tabs/wrapper.html" $params }}
6 changes: 6 additions & 0 deletions layouts/partials/tabs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Tabs

Some credits:

* Radio tabs: https://www.codeinwp.com/snippets/accessible-pure-css-tabs/
* Hugo scaffold: https://blog.jverkamp.com/2021/01/27/a-tabbed-view-for-hugo/
Loading