Sha256: 932d966e81dfbfc8379283a0219a9d60c175201170fa62d02204877242a475c5

Contents?: true

Size: 1.77 KB

Versions: 6

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require "doc2text"
require "tempfile"

module Decidim
  module EnhancedTextwork
    # This class parses a participatory text document in markdown and
    # produces Paragraphs in the form of sections and articles.
    #
    # This implementation uses Redcarpet Base renderer.
    # Redcarpet::Render::Base performs a callback for every block it finds, what MarkdownToParagraphs
    # does is to implement callbacks for the blocks which it is interested in performing some actions.
    #
    class DocToMarkdown
      MARKDOWN_MIME_TYPE = "text/markdown"
      # sometimes markdown documents are classified as text/plain
      TEXT_PLAIN_MIME_TYPE = "text/plain"
      ODT_MIME_TYPE = "application/vnd.oasis.opendocument.text"
      DOCX_MIME_TYPE = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"

      # Accepted mime types
      # keys: are used for dynamic help text on admin form.
      # values: are used to validate the file format of imported document.
      ACCEPTED_MIME_TYPES = {
        md: MARKDOWN_MIME_TYPE,
        odt: ODT_MIME_TYPE
      }.freeze

      # Public: Initializes the serializer with a paragraph.
      def initialize(doc, mime_type)
        @doc = doc
        @transformer = case mime_type
                       # when MARKDOWN_MIME_TYPE
                       # no transformer required
                       when ODT_MIME_TYPE
                         # convert libreoffice odt to markdown
                         OdtToMarkdown.new(doc)
                       when DOCX_MIME_TYPE
                         # convert word 2013 docx to markdown
                         DocxToMarkdown.new(doc)
                       end
      end

      def to_md
        @transformer ? @transformer.to_md : @doc
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-enhanced_textwork-1.0.5 lib/decidim/enhanced_textwork/doc_to_markdown.rb
decidim-enhanced_textwork-1.0.4 lib/decidim/enhanced_textwork/doc_to_markdown.rb
decidim-enhanced_textwork-1.0.3 lib/decidim/enhanced_textwork/doc_to_markdown.rb
decidim-enhanced_textwork-1.0.2 lib/decidim/enhanced_textwork/doc_to_markdown.rb
decidim-enhanced_textwork-1.0.1 lib/decidim/enhanced_textwork/doc_to_markdown.rb
decidim-enhanced_textwork-1.0.0 lib/decidim/enhanced_textwork/doc_to_markdown.rb