Class: TinyWiki::App

Inherits:
Sinatra::Base
  • Object
show all
Defined in:
lib/tiny_wiki/app.rb

Defined Under Namespace

Classes: WikiLinkRenderer

Constant Summary collapse

@@markdown =

Create the Redcarpet Markdown object with desired extensions

Redcarpet::Markdown.new(
  markdown_renderer,
  autolink: true,
  tables: true,
  fenced_code_blocks: true,
  strikethrough: true,
  superscript: true,
  highlight: true,
  quote: true,
  footnotes: true
)

Instance Method Summary collapse

Instance Method Details

#all_wiki_pagesObject

Helper to get all wiki pages (filenames without extension, including paths)



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/tiny_wiki/app.rb', line 113

def all_wiki_pages
  # Find all .md files recursively within the wiki_root
  Dir.glob(File.join(settings.wiki_root, '**', '*.md')).map do |file_path|
    # Get the path relative to wiki_root and remove the .md extension
    # Example: /path/to/wiki_root/Folder/Page.md -> Folder/Page
    relative_path_with_ext = file_path.sub("#{settings.wiki_root}/", '')
    relative_path_with_ext.sub(/\.md$/, '')
  end.sort
rescue Errno::ENOENT
  # If the wiki_root directory doesn't exist yet, return an empty array
  []
end

#debug_log(message) ⇒ Object

Helper for conditional debug logging



26
27
28
# File 'lib/tiny_wiki/app.rb', line 26

def debug_log(message)
  puts "DEBUG: #{message}" if settings.debug
end

#markdown_to_html(markdown_content) ⇒ Object

Helper method to convert Markdown to HTML



108
109
110
# File 'lib/tiny_wiki/app.rb', line 108

def markdown_to_html(markdown_content)
  @@markdown.render(markdown_content)
end

#read_page(page_name) ⇒ Object

Helper method to read the content of a wiki page



94
95
96
97
# File 'lib/tiny_wiki/app.rb', line 94

def read_page(page_name)
  path = wiki_file_path(page_name)
  File.exist?(path) ? File.read(path) : nil
end

#wiki_file_path(page_path) ⇒ Object

Helper method to get the full path to a markdown file ‘page_path` is expected to be URL-decoded and can contain slashes.



83
84
85
86
87
88
89
90
91
# File 'lib/tiny_wiki/app.rb', line 83

def wiki_file_path(page_path)
  # Normalize path to prevent directory traversal (e.g., /foo/../bar)
  # Split by '/' and reject empty components, '.' and '..'.
  # Then re-join to form a safe relative path.
  safe_components = page_path.split('/').reject { |c| c.empty? || c == '.' || c == '..' }

  # Reconstruct the path. File.join handles OS-specific separators.
  File.join(settings.wiki_root, "#{safe_components.join('/')}.md")
end

#write_page(page_name, content) ⇒ Object

Helper method to write content to a wiki page



100
101
102
103
104
105
# File 'lib/tiny_wiki/app.rb', line 100

def write_page(page_name, content)
  path = wiki_file_path(page_name)
  # Ensure the directory exists
  FileUtils.mkdir_p(File.dirname(path)) unless File.directory?(File.dirname(path))
  File.write(path, content)
end