From 84543b5cc0681056eb03d46903cee9f31d7e9dc0 Mon Sep 17 00:00:00 2001 From: Sami Samhuri Date: Sat, 7 Feb 2026 17:00:35 -0800 Subject: [PATCH] Finishing touches --- lib/utils/file_writer.rb | 10 +- lib/utils/html_formatter.rb | 91 ------------------- .../02/textmate-insert-text-into-self-down.md | 3 +- 3 files changed, 2 insertions(+), 102 deletions(-) delete mode 100644 lib/utils/html_formatter.rb diff --git a/lib/utils/file_writer.rb b/lib/utils/file_writer.rb index 4bd5444..9dafed7 100644 --- a/lib/utils/file_writer.rb +++ b/lib/utils/file_writer.rb @@ -1,19 +1,11 @@ require 'fileutils' -require_relative 'html_formatter' module Pressa module Utils class FileWriter def self.write(path:, content:, permissions: 0o644) FileUtils.mkdir_p(File.dirname(path)) - - formatted_content = if path.end_with?('.html') - HtmlFormatter.format(content) - else - content - end - - File.write(path, formatted_content, mode: 'w') + File.write(path, content, mode: 'w') File.chmod(permissions, path) end diff --git a/lib/utils/html_formatter.rb b/lib/utils/html_formatter.rb deleted file mode 100644 index 89bf8f9..0000000 --- a/lib/utils/html_formatter.rb +++ /dev/null @@ -1,91 +0,0 @@ -module Pressa - module Utils - module HtmlFormatter - INDENT = 2 - VOID_TAGS = %w[ - area base br col embed hr img input link meta param source track wbr - ].freeze - - PLACEHOLDER_PREFIX = '%%PRESSA_PRESERVE_' - PRESERVE_PATTERNS = [ - /
.*?<\/div>/m, - /
.*?<\/div>\s*<\/div>/m - ].freeze - - def self.format(html) - html_with_placeholders, preserved = preserve_sections(html) - formatted = format_with_indentation(html_with_placeholders) - restore_sections(formatted, preserved) - end - - def self.format_with_indentation(html) - indent_level = 0 - - formatted_lines = split_lines(html).map do |line| - stripped = line.strip - next if stripped.empty? - - decrease_indent = closing_tag?(stripped) - indent_level = [indent_level - INDENT, 0].max if decrease_indent - - content = tag_line?(stripped) ? stripped : line - current_line = (' ' * indent_level) + content - - if tag_line?(stripped) && !decrease_indent && !void_tag?(stripped) && !self_closing?(stripped) - indent_level += INDENT - end - - current_line - end - - formatted_lines.compact.join("\n") - end - - def self.split_lines(html) - html.gsub(/>\s*\n<").split("\n") - end - - def self.tag_line?(line) - line.start_with?('<') && !line.start_with?('') || line.include?('/]+)}, 1]&.downcase - end - - def self.void_tag?(line) - VOID_TAGS.include?(tag_name(line)) - end - - def self.preserve_sections(html) - preserved = {} - index = 0 - - PRESERVE_PATTERNS.each do |pattern| - html = html.gsub(pattern) do |match| - placeholder = "#{PLACEHOLDER_PREFIX}#{index}%%" - preserved[placeholder] = match - index += 1 - placeholder - end - end - - [html, preserved] - end - - def self.restore_sections(html, preserved) - preserved.reduce(html) do |content, (placeholder, original)| - content.gsub(placeholder, original) - end - end - end - end -end diff --git a/posts/2006/02/textmate-insert-text-into-self-down.md b/posts/2006/02/textmate-insert-text-into-self-down.md index 2a404e0..c77be3b 100644 --- a/posts/2006/02/textmate-insert-text-into-self-down.md +++ b/posts/2006/02/textmate-insert-text-into-self-down.md @@ -34,7 +34,7 @@ lines.each_with_index do |line, i| end # return the new text, escaping special chars -print up_line + lines.reverse.to_s.gsub('[$`\\]', '\\\\\1').gsub('\\$\\$', '$') +print up_line + lines.reverse.to_s.gsub(/([$`\\])/, '\\\\\1').gsub(/\$\$/, '$') ```

Save this as a command in your Rails, or syncPeople on Rails, bundle. The command options should be as follows:

@@ -56,4 +56,3 @@ The macro I'm thinking of to invoke this is tab-triggered and will simply:
  • Select to end of file (โ‡งโŒ˜โ†“)
  • Run command "Put in self.down"
  • -