From 1b579312824db55ab74dedf5646c74a8c512afff Mon Sep 17 00:00:00 2001 From: Cian McElhinney Date: Thu, 24 Feb 2022 09:27:02 +0000 Subject: [PATCH 1/4] add basic JSON and YAML rendering in pre tag --- app/helpers/blazer/base_helper.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/helpers/blazer/base_helper.rb b/app/helpers/blazer/base_helper.rb index 9ca9ea5a4..7a4726986 100644 --- a/app/helpers/blazer/base_helper.rb +++ b/app/helpers/blazer/base_helper.rb @@ -24,7 +24,21 @@ def blazer_format_value(key, value) link_to value, value, target: "_blank" end else - value + if key.include?("json") + begin + content_tag(:pre, JSON.pretty_generate(JSON.parse(value))) + rescue JSON::ParserError + content_tag(:pre, value) + end + elsif key.include?("yaml") + begin + content_tag(:pre, YAML.dump(YAML.load(value))) + rescue Psych::SyntaxError + content_tag(:pre, value) + end + else + value + end end end From 56b2634807fa1e318bb36f03c492716077760fe5 Mon Sep 17 00:00:00 2001 From: Cian McElhinney Date: Thu, 24 Feb 2022 13:12:20 +0000 Subject: [PATCH 2/4] Update base_helper.rb --- app/helpers/blazer/base_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/helpers/blazer/base_helper.rb b/app/helpers/blazer/base_helper.rb index 7a4726986..01a13fd39 100644 --- a/app/helpers/blazer/base_helper.rb +++ b/app/helpers/blazer/base_helper.rb @@ -33,7 +33,7 @@ def blazer_format_value(key, value) elsif key.include?("yaml") begin content_tag(:pre, YAML.dump(YAML.load(value))) - rescue Psych::SyntaxError + rescue Exception content_tag(:pre, value) end else From dcb003eff6c21bace26f615f37a3999e3a171476 Mon Sep 17 00:00:00 2001 From: Cian McElhinney Date: Sat, 26 Feb 2022 12:20:02 +0000 Subject: [PATCH 3/4] use `hljs` to format JSON/YAML on edit + show pages --- app/helpers/blazer/base_helper.rb | 30 ++++++++++++++++--------- app/views/blazer/queries/_form.html.erb | 5 +++++ app/views/blazer/queries/show.html.erb | 5 +++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/app/helpers/blazer/base_helper.rb b/app/helpers/blazer/base_helper.rb index 7a4726986..40924e334 100644 --- a/app/helpers/blazer/base_helper.rb +++ b/app/helpers/blazer/base_helper.rb @@ -25,17 +25,9 @@ def blazer_format_value(key, value) end else if key.include?("json") - begin - content_tag(:pre, JSON.pretty_generate(JSON.parse(value))) - rescue JSON::ParserError - content_tag(:pre, value) - end + content_tag(:pre, format_json_value(value), class: "hljs") elsif key.include?("yaml") - begin - content_tag(:pre, YAML.dump(YAML.load(value))) - rescue Psych::SyntaxError - content_tag(:pre, value) - end + content_tag(:pre, format_yaml_value(value), class: "hljs") else value end @@ -53,5 +45,23 @@ def blazer_js_var(name, value) def blazer_series_name(k) k.nil? ? "null" : k.to_s end + + private + + def format_json_value(value) + begin + JSON.pretty_generate(JSON.parse(value)) + rescue JSON::ParserError + value + end + end + + def format_yaml_value(value) + begin + YAML.dump(YAML.load(value)) + rescue Exception + value + end + end end end diff --git a/app/views/blazer/queries/_form.html.erb b/app/views/blazer/queries/_form.html.erb index 7585a1dc6..b00d76af6 100644 --- a/app/views/blazer/queries/_form.html.erb +++ b/app/views/blazer/queries/_form.html.erb @@ -219,6 +219,11 @@ Vue.nextTick(function () { $("#results-html").html(data) + document.querySelectorAll("#results-html .hljs").forEach((item)=>{ + if(item.textContent.length < 5000) { + hljs.highlightBlock(item) + } + }); }) } }, diff --git a/app/views/blazer/queries/show.html.erb b/app/views/blazer/queries/show.html.erb index c04a59bcf..24b9f3c62 100644 --- a/app/views/blazer/queries/show.html.erb +++ b/app/views/blazer/queries/show.html.erb @@ -50,6 +50,11 @@ function showRun(data) { $("#results").html(data) $("#results table").stupidtable(stupidtableCustomSettings).stickyTableHeaders({fixedOffset: 60}) + document.querySelectorAll("#results .hljs").forEach((item)=>{ + if(item.textContent.length < 5000) { + hljs.highlightBlock(item) + } + }); } function showError(message) { From a4611788c2438036345381eda157e40f4be221e6 Mon Sep 17 00:00:00 2001 From: Cian McElhinney Date: Sun, 26 Nov 2023 10:40:50 +0000 Subject: [PATCH 4/4] Update base_helper.rb --- app/helpers/blazer/base_helper.rb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/app/helpers/blazer/base_helper.rb b/app/helpers/blazer/base_helper.rb index 40924e334..42144c25b 100644 --- a/app/helpers/blazer/base_helper.rb +++ b/app/helpers/blazer/base_helper.rb @@ -49,19 +49,15 @@ def blazer_series_name(k) private def format_json_value(value) - begin - JSON.pretty_generate(JSON.parse(value)) - rescue JSON::ParserError - value - end + JSON.pretty_generate(JSON.parse(value)) + rescue JSON::ParserError + value end def format_yaml_value(value) - begin YAML.dump(YAML.load(value)) - rescue Exception - value - end + rescue StandardError + value end end end