~chambln/public-inbox

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch

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

Peter Marinov <pmar21@sonic.net>
Details
Message ID
<20210519035630.GA170965@presidio-x1>
DKIM signature
missing
Download raw message
Patch: +86 -0
* 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
Reply to thread Export thread (mbox)