<commit_before># coding: utf-8

require 'rubygems'
require 'rack'
require 'sass'
require 'uri'

require 'tilt-mustache'
require 'mdmenu'


class MemoApp
	DEFAULT_APP_OPTIONS = {
		root:			'views/',
		themes_folder:	'themes/',
		theme:			'default',
		markdown:		'redcarpet',
		title:			'memo'
	}

	# テンプレートエンジンのオプション
	DEFAULT_TEMPLATE_OPTIONS = {
		tables:			true
	}

	DEFAULT_OPTIONS = DEFAULT_APP_OPTIONS.merge(DEFAULT_TEMPLATE_OPTIONS)

	def initialize(app, options={})
		options = DEFAULT_OPTIONS.merge(to_sym_keys(options))

		@themes_folders = [options[:themes_folder], File.expand_path('../themes/', __FILE__)]
		read_config(options[:theme], options)

		use_engine(options[:markdown])

		@app = app
		@options = options
		@root = options[:root]
		@title = options[:title]

		define_statics(@root, *@themes)

		# @options からテンプレートで使わないものを削除
		DEFAULT_APP_OPTIONS.each { |key, item| @options.delete(key) }

		# ファイル監視を行う
		watcher(@root)
	end

	def call(env)
		content_type = 'text/html'

		path_info = URI.unescape(env['PATH_INFO'])
		path, ext = split_extname(path_info)

		case path_info
		when '/'
			content = render_with_mustache :index, :markdown
		when /\.css$/
			result = pass(env, @statics)
			return result unless result.first == 404

			content_type = 'text/css'
			content = render :scss, "#{path}.scss", {views: @themes, cache_location: './tmp/sass-cache'}
		else
			return pass(env) unless ext && Tilt.registered?(ext)
			content = render_with_mustache path.to_sym, ext
		end

		return pass(env) unless content

		[200, {'Content-Type' => content_type}, [content.to_s]]
	end


	# リダイレクト
	def redirect(url, code = 301)
		# 301 = 恒久的, 302 = 一時的, 303, 410
		[code, {'Content-Type' => 'text/html', 'Location' => url}, ['Redirect: ', url]]
	end

	# テンプレートエンジンを使用できるようにする
	def use_engine(engine)
		require engine if engine

		# Tilt で Redcarpet 2.x を使うためのおまじない
		Object.send(:remove_const, :RedcarpetCompat) if defined?(RedcarpetCompat) == 'constant'<commit_msg>url に ?edit があった場合のコードを仮追加<commit_after># coding: utf-8

require 'rubygems'
require 'rack'
require 'sass'
require 'uri'

require 'tilt-mustache'
require 'mdmenu'


class MemoApp
	DEFAULT_APP_OPTIONS = {
		root:			'views/',
		themes_folder:	'themes/',
		theme:			'default',
		markdown:		'redcarpet',
		title:			'memo'
	}

	# テンプレートエンジンのオプション
	DEFAULT_TEMPLATE_OPTIONS = {
		tables:			true
	}

	DEFAULT_OPTIONS = DEFAULT_APP_OPTIONS.merge(DEFAULT_TEMPLATE_OPTIONS)

	def initialize(app, options={})
		options = DEFAULT_OPTIONS.merge(to_sym_keys(options))

		@themes_folders = [options[:themes_folder], File.expand_path('../themes/', __FILE__)]
		read_config(options[:theme], options)

		use_engine(options[:markdown])

		@app = app
		@options = options
		@root = options[:root]
		@title = options[:title]

		define_statics(@root, *@themes)

		# @options からテンプレートで使わないものを削除
		DEFAULT_APP_OPTIONS.each { |key, item| @options.delete(key) }

		# ファイル監視を行う
		watcher(@root)
	end

	def call(env)
		content_type = 'text/html'

		req = Rack::Request.new(env)
		query = Rack::Utils.parse_query(req.query_string)
		path_info = URI.unescape(req.path_info)
		path, ext = split_extname(path_info)

		case path_info
		when '/'
			content = render_with_mustache :index, :markdown
		when /\.css$/
			result = pass(env, @statics)
			return result unless result.first == 404

			content_type = 'text/css'
			content = render :scss, "#{path}.scss", {views: @themes, cache_location: './tmp/sass-cache'}
		else
			return pass(env) unless ext && Tilt.registered?(ext)

			if query.has_key?('edit')
				fullpath = File.expand_path(File.join(@root, path_info))

				# @attention リダイレクトはうまく動作しない
				#
				# redirect_url = 'file://' + File.expand_path(File.join(@root, req.path_info))
				# return redirect(redirect_url, 302) if File.exists?(fullpath)
			end

			content = render_with_mustache path.to_sym, ext
		end

		return pass(env) unless content

		[200, {'Content-Type' => content_type}, [content.to_s]]
	end


	# リダイレクト
	def redirect(url, code = 301)
		# 301 = 恒久的, 302 = 一時的, 303, 410
		[code, {'Content-Type' => 'text/html', 'Location' => url}, ['Redirect: ', url]]
	end

	# テンプレートエンジンを使用できるようにする
	def use_engine(engine)
		require engine if engine

		# Tilt で Redcarpet 2.x を使うためのおまじない
		Object.send(:remove_const, :RedcarpetCompat) if defined?(RedcarpetCompat) == 'constant'
{'fraction_non_alphanumeric': 0.1389017074296262, 'fraction_numerical': 0.011305952930318412, 'mean_word_length': 4.3125, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '757868', 'n_tokens_mistral': 1970, 'n_tokens_neox': 1706, 'n_words': 337}