Skip to content

Commit

Permalink
Enable assigning custom CSS & JS as content or URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
xwmx committed Nov 7, 2023
1 parent 4ff8fde commit 8446c0a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
26 changes: 22 additions & 4 deletions nb
Original file line number Diff line number Diff line change
Expand Up @@ -10599,8 +10599,10 @@ _render() {
_render_print_head() {
local _custom_css="${NB_CUSTOM_CSS:-}"
local _custom_css_element=
local _custom_css_url="${NB_CUSTOM_CSS_URL:-}"
local _custom_javascript="${NB_CUSTOM_JAVASCRIPT:-${NB_CUSTOM_JS:-}}"
local _custom_javascript_element=
local _custom_javascript_url="${NB_CUSTOM_JAVASCRIPT_URL:-${NB_CUSTOM_JS_URL:-}}"
local _html_title="${_ME}"

if [[ "${1:-}" == "--title" ]] && [[ -n "${2:-}" ]]
Expand Down Expand Up @@ -10663,14 +10665,28 @@ _render() {
esac
fi

if [[ -n "${_custom_javascript:-}" ]]
if [[ -n "${_custom_javascript:-}" ]]
then
_custom_javascript_element="<script src='${_custom_javascript:-}'></script>"
_custom_javascript_element="\
<script>${_NEWLINE}${_custom_javascript:-}${_NEWLINE}</script>"
fi

if [[ -n "${_custom_css:-}" ]]
if [[ -n "${_custom_javascript_url:-}" ]]
then
_custom_css_element="<link rel='stylesheet' href='${_custom_css:-}'/>"
_custom_javascript_url_element="\
<script src='${_custom_javascript_url:-}'></script>"
fi

if [[ -n "${_custom_css:-}" ]]
then
_custom_css_element="\
<style>${_NEWLINE}${_custom_css:-}${_NEWLINE}</style>"
fi

if [[ -n "${_custom_css_url:-}" ]]
then
_custom_css_url_element="\
<link rel='stylesheet' href='${_custom_css_url:-}'/>"
fi

cat <<HEREDOC
Expand Down Expand Up @@ -11352,6 +11368,8 @@ document.addEventListener('keydown', function(event) {
}
});
</script>
${_custom_css_url_element:-}
${_custom_javascript_url_element:-}
${_custom_css_element:-}
${_custom_javascript_element:-}
<meta name="referrer" content="no-referrer" />
Expand Down
57 changes: 48 additions & 9 deletions test/browse.bats
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ load test_helper
sleep 1

declare _custom_css=".example { color: blue; }"
declare _custom_js=
_custom_js="$(cat <<HEREDOC
function example() { console.log("Example."); }
HEREDOC
)"
declare _custom_js="function example() { console.log(\"Example.\"); }"

declare _custom_css_url="//--original/home/example.css"
declare _custom_js_url="//--original/home/example.js"
}

NB_CUSTOM_CSS="${_custom_css:-}" \
Expand All @@ -45,10 +44,12 @@ HEREDOC

[[ "${status}" -eq 0 ]]

[[ ! "${output}" =~ \<link\ rel=\'stylesheet\' ]]
[[ ! "${output}" =~ \<script\ src= ]]
[[ "${output}" =~ \
\<link\ rel=\'stylesheet\'\ href=\'.example\ {\ color:\ blue\;\ }\'/\> ]]
\<style\>${_NEWLINE}.example\ {\ color:\ blue\;\ }${_NEWLINE}\</style\> ]]
[[ "${output}" =~ \
\<script\ src=\'function\ example\(\)\ {\ console.log\(\"Example.\"\)\;\ }\'\>\</script\> ]]
\<script\>${_NEWLINE}function\ example\(\)\ {\ console.log\(\"Example.\"\)\;\ }${_NEWLINE}\</script\> ]]

NB_CUSTOM_CSS="${_custom_css:-}" \
NB_CUSTOM_JAVASCRIPT="${_custom_js:-}" \
Expand All @@ -59,10 +60,48 @@ HEREDOC

[[ "${status}" -eq 0 ]]

[[ ! "${output}" =~ \<link\ rel=\'stylesheet\' ]]
[[ ! "${output}" =~ \<script\ src= ]]
[[ "${output}" =~ \
\<style\>${_NEWLINE}.example\ {\ color:\ blue\;\ }${_NEWLINE}\</style\> ]]
[[ "${output}" =~ \
\<script\>${_NEWLINE}function\ example\(\)\ {\ console.log\(\"Example.\"\)\;\ }${_NEWLINE}\</script\> ]]

NB_CUSTOM_CSS_URL="${_custom_css_url:-}" \
NB_CUSTOM_JS_URL="${_custom_js_url:-}" \
run "${_NB}" browse --print

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

[[ "${status}" -eq 0 ]]

[[ "${output}" =~ \
\<link\ rel=\'stylesheet\'\ href=\'//--original/home/example.css\'/\> ]]
[[ "${output}" =~ \
\<script\ src=\'//--original/home/example.js\'\>\</script\> ]]
[[ ! "${output}" =~ \
\<style\>${_NEWLINE}.example\ {\ color:\ blue\;\ }${_NEWLINE}\</style\> ]]
[[ ! "${output}" =~ \
\<script\>${_NEWLINE}function\ example\(\)\ {\ console.log\(\"Example.\"\)\;\ }${_NEWLINE}\</script\> ]]

NB_CUSTOM_CSS_URL="${_custom_css_url:-}" \
NB_CUSTOM_JAVASCRIPT_URL="${_custom_js_url:-}" \
run "${_NB}" browse --print

printf "\${status}: '%s'\\n" "${status}"
printf "\${output}: '%s'\\n" "${output}"

[[ "${status}" -eq 0 ]]

[[ "${output}" =~ \
\<link\ rel=\'stylesheet\'\ href=\'.example\ {\ color:\ blue\;\ }\'/\> ]]
\<link\ rel=\'stylesheet\'\ href=\'//--original/home/example.css\'/\> ]]
[[ "${output}" =~ \
\<script\ src=\'function\ example\(\)\ {\ console.log\(\"Example.\"\)\;\ }\'\>\</script\> ]]
\<script\ src=\'//--original/home/example.js\'\>\</script\> ]]
[[ ! "${output}" =~ \
\<style\>${_NEWLINE}.example\ {\ color:\ blue\;\ }${_NEWLINE}\</style\> ]]
[[ ! "${output}" =~ \
\<script\>${_NEWLINE}function\ example\(\)\ {\ console.log\(\"Example.\"\)\;\ }${_NEWLINE}\</script\> ]]
}

# configuration ###############################################################
Expand Down

0 comments on commit 8446c0a

Please sign in to comment.