<commit_before>require 'json'
require 'uri'

require './libs/params'
require './config'

require './libs/page_utils'
require './libs/database'
require './libs/stop_evaluation'
require './libs/ginger_parser'
require './libs/HTMLGenerator.rb'
require './libs/CSVGenerator.rb'
require './libs/common_utils.rb'

require 'rubygems'
require 'sinatra'
require 'redcloth'

base_files_directory = get_conf['base_files_directory']

def template_to_html(content, params)
	convertors = []

	convertors = [
		proc {|content| parse_ginger_doc(content) },
		proc {|content| HTMLGenerator.new(params).generate(content) },
		proc {|content|
			redcloth = RedCloth.new(content)
			redcloth.extend FormTag
			redcloth.to_html(content)
		}
	]

	new_content = content

	convertors.each {|convertor|
		new_content = convertor.call(new_content)
	}

	return new_content
end

def store_param_data(stored_data, template_params, params, name, title, type)
	param_name = "p_#{name}"

	if params.has_key?(param_name) && params[param_name].length > 0
		stored_data[:request_params][name] = {:value => params[param_name], :type => type}
	else
		if (template_params['required'].to_s || "").downcase == 'true'
			raise StopEvaluation.new(title)
		end
	end
end

def add_cache_request(url)
	return if url.index('cache=true') != nil
	return url + '&cache=true' if url.index('?') != nil
	return url + '?cache=true'
end

def remove_cache_request(url, cache_switch)
	return "" if url == nil
	clean_url = url.gsub(/&cache=#{cache_switch}/, "")
	clean_url = clean_url.gsub(/\?cache=#{cache_switch}/, "")
	clean_url = clean_url.gsub(/cache=#{cache_switch}/, "") if clean_url.index("cache=#{cache_switch}") == 0
	clean_url = clean_url.gsub(/\?\s*$/, "")

	return clean_url
end

get '/' do
	@list_of_pages = page.list
	haml :page_list
end

get '/explore/:datasource' do
	template = "[:#{params['datasource']} show tables; :]"

	@page = {}
	@page['content'] = template_to_html(template, params)

	haml :show_page
end

get '/explore/:datasource/:table' do
	template = "[:#{params['datasource']} desc #{params['table']}; :]"

	@page = {}
	@page['content'] = template_to_html(template, params)

	haml :show_page
end

get '/page/:page_id/edit' do
	@page_id = params[:page_id]
	@page = nil

	@page_title = "Edit page"<commit_msg>Improved the datasource explorer for mysql.<commit_after>require 'json'
require 'uri'

require './libs/params'
require './config'

require './libs/page_utils'
require './libs/database'
require './libs/stop_evaluation'
require './libs/ginger_parser'
require './libs/HTMLGenerator.rb'
require './libs/CSVGenerator.rb'
require './libs/common_utils.rb'

require 'rubygems'
require 'sinatra'
require 'redcloth'

base_files_directory = get_conf['base_files_directory']

def template_to_html(content, params)
	convertors = []

	convertors = [
		proc {|content| parse_ginger_doc(content) },
		proc {|content| HTMLGenerator.new(params).generate(content) },
		proc {|content|
			redcloth = RedCloth.new(content)
			redcloth.extend FormTag
			redcloth.to_html(content)
		}
	]

	new_content = content

	convertors.each {|convertor|
		new_content = convertor.call(new_content)
	}

	return new_content
end

def store_param_data(stored_data, template_params, params, name, title, type)
	param_name = "p_#{name}"

	if params.has_key?(param_name) && params[param_name].length > 0
		stored_data[:request_params][name] = {:value => params[param_name], :type => type}
	else
		if (template_params['required'].to_s || "").downcase == 'true'
			raise StopEvaluation.new(title)
		end
	end
end

def add_cache_request(url)
	return if url.index('cache=true') != nil
	return url + '&cache=true' if url.index('?') != nil
	return url + '?cache=true'
end

def remove_cache_request(url, cache_switch)
	return "" if url == nil
	clean_url = url.gsub(/&cache=#{cache_switch}/, "")
	clean_url = clean_url.gsub(/\?cache=#{cache_switch}/, "")
	clean_url = clean_url.gsub(/cache=#{cache_switch}/, "") if clean_url.index("cache=#{cache_switch}") == 0
	clean_url = clean_url.gsub(/\?\s*$/, "")

	return clean_url
end

get '/' do
	@list_of_pages = page.list
	haml :page_list
end

get '/explore/:datasource' do
	template = nil

	datasource_name = params['datasource']
	database_type = get_conf['datasources'][datasource_name]['type']
	
	if database_type == 'mysql'
		template = "[:#{datasource_name} when Tables_in_bllinvoices then format:'\"%%\":/explore/#{datasource_name}/%%' show tables; :]"
	elsif database_type == 'psql'
		template = "[:#{datasource_name} \\l :]"
	end

	@page = {}
	@page['content'] = template_to_html(template, params)

	haml :show_page
end

get '/explore/:datasource/:table' do
	template = nil

	datasource_name = params['datasource']
	database_type = get_conf['datasources'][datasource_name]['type']
	
	if database_type == 'mysql'
		template = "[:#{params['datasource']} desc #{params['table']}; :]"
	elsif database_type == 'psql'
		template = "[:#{params['datasource']} \d #{params['table']} :]"
	end

	@page = {}
	@page['content'] = template_to_html(template, params)

	haml :show_page
end

get '/page/:page_id/edit' do
	@page_id = params[:page_id]
	@page = nil

	@page_title = "Edit page"
{'fraction_non_alphanumeric': 0.16514651659227633, 'fraction_numerical': 0.0007762468464971861, 'mean_word_length': 5.209638554216867, 'pattern_counts': {'":': 1, '<': 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': '7668312', 'n_tokens_mistral': 1953, 'n_tokens_neox': 1843, 'n_words': 375}