Skip to content

Commit

Permalink
more form
Browse files Browse the repository at this point in the history
  • Loading branch information
bollwyvl committed Dec 3, 2023
1 parent 9145739 commit 8da549b
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 35 deletions.
2 changes: 1 addition & 1 deletion docs/_templates/demo.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="work-sidebar {% if not lite_links %}work-sidebar-fail{% endif %}">
<div class="work-sidebar-header">
<i class="fas fa-{% if lite_links %}square-arrow-up-right{% else %}bomb{% endif %}"></i>
Preview Site
current preview site
</div>
<div class="work-sidebar-content">
<blockquote>
Expand Down
11 changes: 2 additions & 9 deletions docs/_templates/new-form.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
<style>
.bd-footer,
.header-article-items {
display: none;
}
.bd-main .bd-content .bd-article-container .bd-article {
padding: 0;
}
.bd-sidebar-primary {
padding-bottom: 0;
}
footer {
display: none;
}
</style>

<div class="work-sidebar">
<div class="work-sidebar-header">
<i class="fas fa-code-pull-request"></i>
Request Preview Site
start pull request
</div>
<div class="work-sidebar-content">
<form id="propose" method="get" action="https://github.com/deathbeds/jupyak/new/main" target="_blank">
Expand Down Expand Up @@ -45,7 +38,7 @@
}
</script>
<script type="module">
document.addEventListener('DOMContentLoaded', async () => {
document.addEventListener('DOMContentLoaded', async function(){
const form = document.querySelector('form#new');
const preview = document.querySelector('#toml-preview');
const selector = 'form#new input[type="text"], form#new select';
Expand Down
6 changes: 0 additions & 6 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

# sphinx config
extensions = [
"sphinx.ext.intersphinx",
"sphinx.ext.viewcode",
"myst_nb",
"sphinx.ext.autosectionlabel",
Expand All @@ -55,10 +54,6 @@
autosectionlabel_prefix_document = True
myst_heading_anchors = 3

intersphinx_mapping = {
"python": ("https://docs.python.org/3", None),
}

mermaid_version = ""
mermaid_init_js = "false"

Expand Down Expand Up @@ -102,7 +97,6 @@
html_sidebars = {
"*": ["page-toc", "edit-this-page", "sourcelink"],
"graph": [],
"new": ["new-form"],
}

if REPO_INFO is not None:
Expand Down
119 changes: 116 additions & 3 deletions docs/new.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# request a preview site

> make selections below, then use the sidebar to start a new Pull Request
> Make selections below for the most common configuration options, then use the
> _start pull request_ form to be redirected to GitHub.
> A large number of other configuration options are _possible_, but are not yet fully described.
<style>

Expand Down Expand Up @@ -88,7 +90,15 @@ display: none;
</style>
<form id="new">

<h2>repos</h2>
<fieldset>
<legend><h2>customize GitHub checkout</h2></legend>

<blockquote>
All of the repositories below will be checked out at the current <code>main</code> branch as a <em>baseline</em>.
Check one or more repos to provide use a differnt branch, PR, or tag, or provide one or more PRs to
<i>merge with</i> it, and/or customize the <a href="https://git-scm.com/docs/merge-strategies" target="blank">merge strategy and options</a> to work around merge conflicts.
</blockquote>


<input type="checkbox" name="show-repo-traitlets" id="show-repo-traitlets"/>
<label class="show-repo-label" for="show-repo-traitlets">traitlets</label>
Expand Down Expand Up @@ -1361,4 +1371,107 @@ title="add space-delimted -X options for the merge strategy"

</tbody>
</table>
</form>

<fieldset>
<legend>customize JupyterLite</legend>

<blockquote>
If given, an optional <a target="_blank" href="https://gist.github.com/">gist</a> will be cloned
to provide the content of the JupyterLite site.
If the gist contains <code>jupyter_lite_config.json</code> and/or <code>jupyter-lite.json</code>,
this will be merged into the generated
<a target="_blank" href="https://jupyterlite.readthedocs.io/en/latest/howto/configure/config_files.html">configuration</a>
of <code>jupyter lite build</code> and the runtime application.
</blockquote>

<input id="lite-gist"
type="text"
name="lite|gist"
pattern="^$|^[a-z\d]{20,}$"
placeholder="gist ID"
/>
<label for="lite-gist">the gist ID must be empty, or contain 20+ letters and numbers</label>
</fieldset>
</form>
<style>
.bd-main .bd-content .bd-article-container .bd-article {
padding: 0;
}
.bd-sidebar-primary {
padding-bottom: 0;
}
</style>

<div class="work-sidebar">
<div class="work-sidebar-header">
<i class="fas fa-code-pull-request"></i>
start pull request
</div>
<div class="work-sidebar-content">
<form id="propose" method="get" action="https://github.com/deathbeds/jupyak/new/main" target="_blank">
<label for="toml-preview"><code>jupyak_config.toml</code> contents</label>
<textarea
required="true"
id="toml-preview"
name="value"
spellcheck="false"
placeholder="# make choices in the form"
></textarea>
<input type="hidden" name="filename" value="jupyak_config.toml"/>
<br/>
<button class="btn btn-success" form="propose">
<i class="fas fa-code-pull-request"></i> New Pull Request
</button>
</form>
</div>
</div>
<script type="importmap">
{
"imports": {
"json2toml": "https://cdn.skypack.dev/pin/json2toml@v6.0.0-d8Y8va9lNUE85BZ5GSQ2/mode=imports,min/optimized/json2toml.js"
}
}
</script>
<script type="module">
document.addEventListener('DOMContentLoaded', async function(){
const form = document.querySelector('form#new');
const preview = document.querySelector('#toml-preview');
const selector = 'form#new input[type="text"], form#new select';
const inputs = [...document.querySelectorAll(selector)];
const split_fields = ['merge_with', 'merge_options'];
async function update() {
const json2toml = (await import('json2toml')).default;
const config = find_config();
preview.value = json2toml(config);
}
function find_config() {
const config = {};
const data = new FormData(form);
for (const [name, value] of data.entries()) {
if(document.querySelector(`input[name="${name}"]:invalid`)) {
continue
}
let bits = name.split("|");
if(value.trim() === "" || bits.length == 1){
continue;
}
let current = config;
for(const bit of bits.slice(0, -1)) {
if(!current[bit]) {
current[bit] = {};
}
current = current[bit];
}
let last_bit = bits[bits.length - 1];
current[last_bit] = (
split_fields.includes(last_bit) ?
value.split(/[\s\n]+/):
value
);
}
return config;
}
inputs.map((input) => input.addEventListener('input', update))
})

</script>
88 changes: 72 additions & 16 deletions src/jupyak/tasks/_form.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"from pathlib import Path\n",
"\n",
"import importnb\n",
"import IPython\n",
"import jinja2"
]
},
Expand All @@ -43,7 +44,7 @@
"source": [
"with importnb.Notebook():\n",
" from jupyak.tasks import load_tasks\n",
" from jupyak.tasks._yak import Repo, Yak"
" from jupyak.tasks._yak import Yak"
]
},
{
Expand All @@ -55,18 +56,7 @@
"source": [
"def load_yak():\n",
" load_tasks()\n",
" return Yak({\"pr\": {}})"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "adbc58fe-c2a6-4ef7-859a-a88546a60d15",
"metadata": {},
"outputs": [],
"source": [
"def make_repo(repo: Repo):\n",
" return []"
" return Yak({\"repos\": {}})"
]
},
{
Expand All @@ -77,7 +67,15 @@
"outputs": [],
"source": [
"REPOS_TMPL = r\"\"\"\n",
"<h2>repos</h2>\n",
"<fieldset>\n",
"<legend><h2>customize GitHub checkout</h2></legend>\n",
"\n",
"<blockquote>\n",
"All of the repositories below will be checked out at the current <code>main</code> branch as a <em>baseline</em>.\n",
"Check one or more repos to provide use a differnt branch, PR, or tag, or provide one or more PRs to\n",
"<i>merge with</i> it, and/or customize the <a href=\"https://git-scm.com/docs/merge-strategies\" target=\"blank\">merge strategy and options</a> to work around merge conflicts.\n",
"</blockquote>\n",
"\n",
"{% for name in yak.repos %}\n",
" <input type=\"checkbox\" name=\"show-repo-{{ name }}\" id=\"show-repo-{{ name }}\"/>\n",
" <label class=\"show-repo-label\" for=\"show-repo-{{ name }}\">{{name}}</label>\n",
Expand Down Expand Up @@ -213,6 +211,48 @@
" return [jinja2.Template(REPO_STYLE_TEMPL).render(yak=yak)]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "80c59837-bc25-4571-9c28-bcb61dac930e",
"metadata": {},
"outputs": [],
"source": [
"LITE_TMPL = r\"\"\"\n",
"<fieldset>\n",
"<legend>customize JupyterLite</legend>\n",
"\n",
"<blockquote>\n",
"If given, an optional <a target=\"_blank\" href=\"https://gist.github.com/\">gist</a> will be cloned\n",
"to provide the content of the JupyterLite site.\n",
"If the gist contains <code>jupyter_lite_config.json</code> and/or <code>jupyter-lite.json</code>,\n",
"this will be merged into the generated\n",
"<a target=\"_blank\" href=\"https://jupyterlite.readthedocs.io/en/latest/howto/configure/config_files.html\">configuration</a>\n",
"of <code>jupyter lite build</code> and the runtime application.\n",
"</blockquote>\n",
"\n",
"<input id=\"lite-gist\"\n",
" type=\"text\"\n",
" name=\"lite|gist\"\n",
" pattern=\"^$|^[a-z\\d]{20,}$\"\n",
" placeholder=\"gist ID\"\n",
" />\n",
"<label for=\"lite-gist\">the gist ID must be empty, or contain 20+ letters and numbers</label>\n",
"</fieldset>\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4b128fc0-36ad-463d-a1ff-8bfe0e3a4ffe",
"metadata": {},
"outputs": [],
"source": [
"def make_lite_form(yak: Yak):\n",
" return [jinja2.Template(LITE_TMPL).render(yak=yak)]"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -225,7 +265,8 @@
" chunks = []\n",
" style_chunks = []\n",
" script_chunks = []\n",
" for trait_name, value in yak.trait_values().items():\n",
" for trait_name in [\"repos\", \"lite\"]:\n",
" value = yak.trait_values()[trait_name]\n",
" maker = globals().get(f\"make_{trait_name}_form\")\n",
" if maker:\n",
" chunks += maker(yak)\n",
Expand Down Expand Up @@ -253,16 +294,31 @@
"source": [
"def write_form(dest: Path, **options):\n",
" dest.parent.mkdir(parents=True, exist_ok=True)\n",
" final_tmpl = dest.parent / \"_templates/new-form.html\"\n",
" chunks = [\n",
" \"# request a preview site\",\n",
" \"\",\n",
" \"> make selections below, then use the sidebar to start a new Pull Request\",\n",
" \"> Make selections below for the most common configuration options, then use the \",\n",
" \"> _start pull request_ form to be redirected to GitHub.\",\n",
" \"> A large number of other configuration options are _possible_, but are not yet fully described.\",\n",
" \"\",\n",
" *make_form(),\n",
" final_tmpl.read_text(encoding=\"utf-8\"),\n",
" ]\n",
" txt = re.sub(r\"^ +\", \"\", \"\\n\".join(chunks), flags=re.M)\n",
" dest.write_text(txt, encoding=\"utf-8\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ac0c6c3-a69b-4e5e-bf55-1dcc2449f283",
"metadata": {},
"outputs": [],
"source": [
"if __name__ == \"__main__\":\n",
" IPython.display.display(IPython.display.HTML(\"\\n\".join(make_form())))"
]
}
],
"metadata": {
Expand Down

0 comments on commit 8da549b

Please sign in to comment.