~chambln/public-inbox

Add syntax coloring for Gemini markup via `gawk` v1 PROPOSED

Peter Marinov
Peter Marinov: 1
 Add syntax coloring for Gemini markup via `gawk`

 1 files changed, 86 insertions(+), 0 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~chambln/public-inbox/patches/22804/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH] Add syntax coloring for Gemini markup via `gawk` Export this patch

Peter Marinov
* Works with GNU Awk
---
 contrib/gmi_color_gawk.awk | 86 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 86 insertions(+)
 create mode 100644 contrib/gmi_color_gawk.awk

diff --git a/contrib/gmi_color_gawk.awk b/contrib/gmi_color_gawk.awk
new file mode 100644
index 0000000..787ed6b
--- /dev/null
+++ b/contrib/gmi_color_gawk.awk
@@ -0,0 +1,86 @@
# gmi_color_gawk.awk
#
# Markup highlighting of Gemini pages for ANSI terminal
#
# IMPORTANT:
# It uses GNU Awk syntax, it won't work with mawk for example

func print_folded(long_line, prefix) {
  # Split words into an array
  split(long_line, list_of_words)

  line_len = 0
  for(i in list_of_words) {
    # Check if we go beyond width of the block
    if ((line_len + length(list_of_words[i])) > 78) {
      line_len = 0
      printf("\n")
    }
    # Print prefix
    if (line_len == 0)
      printf(prefix)
    line_len += length(list_of_words[i]) + 1
    printf("%s ", list_of_words[i])
  }

  # Handle case of empty lines
  if (length(long_line) == 0)
    printf(prefix)

  # The line ends with a new-line
  printf("\n")
}

BEGIN {
  code_section = 0
}

# URL
match($0, /^(=>) ([^ \t]+).(.*)/, arr) {
  if (code_section == 0)
  {
    print "\033[0;36m" arr[1], arr[2] "\033[0;35m,\n\t\033[1m", arr[3] "\033[0m"
    next
  }
}

# Quote
match($0, /^>(.*)/, arr) {
  if (code_section == 0)
  {
    printf("\033[0;34m")
    print_folded(arr[1], "> ")
    printf("\033[0m")
    next
  }
}

# Code section
/^```/ {
  if (code_section == 0)
      # Activate color for code section
      print "\033[0;32m```"
  else
      # Remove color at the end of the code section
      print "```\033[0m"
  code_section = 1 - code_section
  next
}

# Heading
/^#.*/ {
  if (code_section == 0)
  {
    print "\033[0m\033[1m" $0 "\033[0m"
    next
  }
}

# Everything else -- the plain text of the file
{
  # Outaide code sections: Wrap long lines, print short lines and as-is
  if (code_section == 0)
    print_folded($0, "\033[0m")
  else
    print("\033[0;32m" $0)
}
-- 
2.25.1