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)



88
89
90
91
92
93
94
95
# File 'lib/tiny_wiki/app.rb', line 88

def all_wiki_pages
  Dir.glob(File.join(settings.wiki_root, '*.md')).map do |file|
    File.basename(file, '.md')
  end.sort
rescue Errno::ENOENT
  # If the wiki_root directory doesn't exist yet, return an empty array
  []
end

#markdown_to_html(markdown_content) ⇒ Object

Helper method to convert Markdown to HTML



83
84
85
# File 'lib/tiny_wiki/app.rb', line 83

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



69
70
71
72
# File 'lib/tiny_wiki/app.rb', line 69

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

#wiki_file_path(page_name) ⇒ Object

Helper method to get the full path to a markdown file ‘page_name` is expected to be URL-decoded and safe for filenames.



61
62
63
64
65
66
# File 'lib/tiny_wiki/app.rb', line 61

def wiki_file_path(page_name)
  # Ensure the page name doesn't contain directory traversal attempts
  # This is a basic sanitization. For production, more robust checks are needed.
  sanitized_page_name = page_name.gsub(/[^a-zA-Z0-9_\-]/, '') # Allow only alphanumeric, underscore, hyphen
  File.join(settings.wiki_root, "#{sanitized_page_name}.md")
end

#write_page(page_name, content) ⇒ Object

Helper method to write content to a wiki page



75
76
77
78
79
80
# File 'lib/tiny_wiki/app.rb', line 75

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