<commit_before># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# HTML output facilities
module html

# A html page
#
# You can define subclass and override methods head and body
#
# class MyPage
#	super HTMLPage
#	redef body do add("p").text("Hello World!")
# end
#
# HTMLPage use fluent interface so you can chain calls as:
#	add("div").attr("id", "mydiv").text("My Div")
class HTMLPage

	# Define head content
	fun head do end
	# Define body content
	fun body do end

	private var root = new HTMLTag("html")
	private var current: HTMLTag = root
	private var stack = new List[HTMLTag]

	# Render the page as a html string
	fun render: String do
		root.children.clear
		open("head")
		head
		close("head")
		open("body")
		body
		close("body")
		return "<!DOCTYPE html>{root.html}"
	end

	# Add a html tag to the current element
	# add("div").attr("id", "mydiv").text("My Div")
	fun add(tag: String): HTMLTag do
		var node = new HTMLTag(tag)
		current.add(node)
		return node
	end

	# Add a raw html string
	# add_html("<a href='#top'>top</a>")
	fun add_html(html: String) do current.add(new HTMLRaw(html))

	# Open a html tag
	# open("ul")
	# add("li").text("item1")
	# add("li").text("item2")
	# close("ul")
	fun open(tag: String): HTMLTag do
		stack.push(current)
		current = add(tag)
		return current
	end

	# Close previously opened tag
	# Ensure: tag = previous.tag
	fun close(tag: String) do
		if not tag == current.tag then
			print "Error: Trying to close '{tag}', last opened tag was '{current.tag}'."
			abort
		end
		current = stack.pop
	end

	# Save html page in the specified file
	fun save(file: String) do
		var out = new OFStream.open(file)
		out.write(self.render)
		out.close
	end
end

class HTMLTag
	# HTML tagname: 'div' for <div></div>
	var tag: String
	init(tag: String) do self.tag = tag

	init with_attrs(tag: String, attrs: Map[String, String]) do
		self.tag = tag
		self.attrs = attrs
	end

	# Tag attributes map
	var attrs: Map[String, String] = new HashMap[String, String]

	# Get the attributed value of 'prop' or null if 'prop' is undifened
	fun get_attr(key: String): nullable String do
		if not attrs.has_key(key) then return null
		return attrs[key]
	end

	# Set a 'value' for 'key'
	# var img = new HTMLTag("img")
	# img.attr("src", "./image.png").attr("alt", "image")
	fun attr(key: String, value: String): HTMLTag do
		attrs[key] = value
		return self
	end

	# Add a CSS class to the HTML tag
	# var img = new HTMLTag("img")
	# img.add_class("logo").add_class("fullpage")
	fun add_class(klass: String): HTMLTag do
		classes.add(klass)
		return self
	end
	private var classes: Set[String] = new HashSet[String]

	# Add multiple CSS classes
	fun add_classes(classes: Collection[String]): HTMLTag do<commit_msg>libhtml: gives access to HTMLTag#classes<commit_after># This file is part of NIT ( http://www.nitlanguage.org ).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# HTML output facilities
module html

# A html page
#
# You can define subclass and override methods head and body
#
# class MyPage
#	super HTMLPage
#	redef body do add("p").text("Hello World!")
# end
#
# HTMLPage use fluent interface so you can chain calls as:
#	add("div").attr("id", "mydiv").text("My Div")
class HTMLPage

	# Define head content
	fun head do end
	# Define body content
	fun body do end

	private var root = new HTMLTag("html")
	private var current: HTMLTag = root
	private var stack = new List[HTMLTag]

	# Render the page as a html string
	fun render: String do
		root.children.clear
		open("head")
		head
		close("head")
		open("body")
		body
		close("body")
		return "<!DOCTYPE html>{root.html}"
	end

	# Add a html tag to the current element
	# add("div").attr("id", "mydiv").text("My Div")
	fun add(tag: String): HTMLTag do
		var node = new HTMLTag(tag)
		current.add(node)
		return node
	end

	# Add a raw html string
	# add_html("<a href='#top'>top</a>")
	fun add_html(html: String) do current.add(new HTMLRaw(html))

	# Open a html tag
	# open("ul")
	# add("li").text("item1")
	# add("li").text("item2")
	# close("ul")
	fun open(tag: String): HTMLTag do
		stack.push(current)
		current = add(tag)
		return current
	end

	# Close previously opened tag
	# Ensure: tag = previous.tag
	fun close(tag: String) do
		if not tag == current.tag then
			print "Error: Trying to close '{tag}', last opened tag was '{current.tag}'."
			abort
		end
		current = stack.pop
	end

	# Save html page in the specified file
	fun save(file: String) do
		var out = new OFStream.open(file)
		out.write(self.render)
		out.close
	end
end

class HTMLTag
	# HTML tagname: 'div' for <div></div>
	var tag: String
	init(tag: String) do self.tag = tag

	init with_attrs(tag: String, attrs: Map[String, String]) do
		self.tag = tag
		self.attrs = attrs
	end

	# Tag attributes map
	var attrs: Map[String, String] = new HashMap[String, String]

	# Get the attributed value of 'prop' or null if 'prop' is undifened
	fun get_attr(key: String): nullable String do
		if not attrs.has_key(key) then return null
		return attrs[key]
	end

	# Set a 'value' for 'key'
	# var img = new HTMLTag("img")
	# img.attr("src", "./image.png").attr("alt", "image")
	fun attr(key: String, value: String): HTMLTag do
		attrs[key] = value
		return self
	end

	# Add a CSS class to the HTML tag
	# var img = new HTMLTag("img")
	# img.add_class("logo").add_class("fullpage")
	fun add_class(klass: String): HTMLTag do
		classes.add(klass)
		return self
	end
	var classes: Set[String] = new HashSet[String]

	# Add multiple CSS classes
	fun add_classes(classes: Collection[String]): HTMLTag do
{'fraction_non_alphanumeric': 0.11773584905660377, 'fraction_numerical': 0.0018113207547169811, 'mean_word_length': 3.9374068554396424, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '1816400', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2156, 'n_words': 826}