<commit_before>require 'opml-parser/version'

autoload :OpmlParser, 'opml-parser/opml_parser'<commit_msg>fixed a bug in the library code + change lib/ organization<commit_after>require 'nokogiri'
require 'opml-parser/version'

# OpmlParser Module
#
# A very simple and easy to use module for importing and exporting OPML.
#
# == Credits
#
# <NAME> <<EMAIL>>
#
module OpmlParser
	# Class representing an OPML outline element
	class Outline
		# OPML outline attributes (generally text, title, type, xmlUrl
		# and htmlUrl)
		attr_reader :attributes

		# Initializes an outline object
		#
		# Arguments:
		# 	attributes: (Hash) A Hash table that contains the attributes of the
		# 	outline.
		#
		def initialize(attributes=Hash.new)
			@attributes = attributes
		end
	end

	# Import an OPML String as an array of Outline object
	#
	# Arguments:
	# 	contents: (String) A String that contains the OMPL
	def import(contents)
		doc = Nokogiri.XML(contents)

		doc.xpath("//body//outline").inject(Array.new) do |feeds, outline|
			next if outline.attributes.empty?
			feeds << Outline.new(xml_node_to_hash(outline.attributes))
		end
	end

	# Import an OPML String as an array of Outline object
	#
	# Arguments:
	# 	feeds: (Array of Outline) An array of Outline objects
	def export(feeds)
		builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
			xml.opml(version: "1.0") do
				xml.head { xml.title "RReader Exported Feeds" }
				xml.body { feeds.each { |outline| xml.outline(outline.attributes) }}
			end
		end
		builder.to_xml
	end

	private
		# Convert a Nokogiri XML Node to a Hash table
		def xml_node_to_hash(nokogiri_node)
			attributes = Hash.new

			nokogiri_node.each do |k,v|
				attributes[k.to_sym] = v.value
			end

			return attributes
		end
end
{'fraction_non_alphanumeric': 0.09750566893424037, 'fraction_numerical': 0.0017006802721088435, 'mean_word_length': 3.835616438356164, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '66981', 'n_tokens_mistral': 666, 'n_tokens_neox': 605, 'n_words': 181}