Class: SlateSerializer::Html
- Inherits:
-
Object
- Object
- SlateSerializer::Html
- Defined in:
- lib/slate_serializer/html.rb
Overview
Html de- and serializer
Constant Summary collapse
- ELEMENTS =
Default lookup list to convert html tags to object types
{ 'a': 'link', 'img': 'image', 'li': 'list-item', 'p': 'paragraph', 'div': 'paragraph', 'ol1': 'ordered-list', 'ola': 'alpha-ordered-list', 'ol': 'ordered-list', 'ul': 'unordered-list', 'table': 'table', 'tbody': 'tbody', 'tr': 'tr', 'td': 'td', 'text': 'text', 'hr': 'hr', 'figure': 'figure', 'figcaption': 'figcaption' }.freeze
- BLOCK_ELEMENTS =
Default block types list
%w[figure figcaption hr img li p ol ul table tbody tr td].freeze
- INLINE_ELEMENTS =
Default inline types list
%w[a].freeze
- MARK_ELEMENTS =
Default mark types list
{ 'em': 'italic', 'strong': 'bold', 'u': 'underline' }.freeze
Class Method Summary collapse
-
.deserializer(html, options = {}) ⇒ Object
Convert html to a Slate document.
-
.serializer(value) ⇒ String
Convert html to a Slate document.
Class Method Details
.deserializer(html, options = {}) ⇒ Object
Convert html to a Slate document
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/slate_serializer/html.rb', line 46 def deserializer(html, = {}) return empty_state if html.nil? || html == '' self.elements = [:elements] || ELEMENTS self.block_elements = [:block_elements] || BLOCK_ELEMENTS self.inline_elements = [:inline_elements] || INLINE_ELEMENTS self.mark_elements = [:mark_elements] || MARK_ELEMENTS html = html.gsub('<br>', "\n") nodes = Nokogiri::HTML.fragment(html).elements.map do |element| element_to_node(element) end { document: { object: 'document', nodes: nodes } } end |
.serializer(value) ⇒ String
Convert html to a Slate document
71 72 73 74 75 |
# File 'lib/slate_serializer/html.rb', line 71 def serializer(value) return '' unless value.key?(:document) serialize_node(value[:document]) end |