<commit_before>require 'diffy'
class Nabu

	route 'p' do |r|
		r.is 'preview' do
			r.halt 403 unless user.is_admin? # we're not a free md renderer.
			response.headers['Content-Type'] = 'text/javascript'
			cpv = CmsPageVersion.new(content: r.params['content'])
			"$('.md-preview').html(#{cpv.render_html.to_json});"
		end

		r.on :pagename do |pagename|

			@page = CmsPage[name: pagename] || CmsPage.new(name: pagename)

			@pv = if user.is_admin? && pv = r.params['pv']
							# facility to view old versions for admin
							CmsPageVersion[page: @page, id: pv] or r.halt 404
						else
							# don't 404 for admins, we may want to create the page
							@page&.latest_visible(user) or (r.halt 404 unless user.is_admin?)
						end

			# For now I intentionally haven't done a #visible check here;
			# if someone knows the name of a page then I probably gave them
			# it so they can view it. See how that logic goes..

			r.is do
				response.cache_control public: true, max_age: 86400
				r.last_modified @pv.created_at

				# caching only active for guests; not admin
				# using r.path as the cache key is for ngx_http_memcached_module
				# (even without using that, this doubles Nabu's throughput)
				cache(r.path, cache: !user.is_admin?) do
					render 'p/view'
				end
			end

			# nothing else below this permitted for non-admin
			r.halt 403 unless user.is_admin?

			r.is 'edit' do
				r.post do
					@page.save if @page.new?
					pv = @page.add_version(user: user,
																 content: r.params['content'],<commit_msg>remove cache_control and fix last_modified for non-existant pages<commit_after>require 'diffy'
class Nabu

	route 'p' do |r|
		r.is 'preview' do
			r.halt 403 unless user.is_admin? # we're not a free md renderer.
			response.headers['Content-Type'] = 'text/javascript'
			cpv = CmsPageVersion.new(content: r.params['content'])
			"$('.md-preview').html(#{cpv.render_html.to_json});"
		end

		r.on :pagename do |pagename|

			@page = CmsPage[name: pagename] || CmsPage.new(name: pagename)

			@pv = if user.is_admin? && pv = r.params['pv']
							# facility to view old versions for admin
							CmsPageVersion[page: @page, id: pv] or r.halt 404
						else
							# don't 404 for admins, we may want to create the page
							@page&.latest_visible(user) or (r.halt 404 unless user.is_admin?)
						end

			# For now I intentionally haven't done a #visible check here;
			# if someone knows the name of a page then I probably gave them
			# it so they can view it. See how that logic goes..

			r.is do
				r.last_modified @pv&.created_at

				# caching only active for guests; not admin
				# using r.path as the cache key is for ngx_http_memcached_module
				# (even without using that, this doubles Nabu's throughput)
				cache(r.path, cache: !user.is_admin?) do
					render 'p/view'
				end
			end

			# nothing else below this permitted for non-admin
			r.halt 403 unless user.is_admin?

			r.is 'edit' do
				r.post do
					@page.save if @page.new?
					pv = @page.add_version(user: user,
																 content: r.params['content'],
{'fraction_non_alphanumeric': 0.10840636157091853, 'fraction_numerical': 0.011359948068808828, 'mean_word_length': 2.906210392902408, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5582085', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1013, 'n_words': 349}