class PnoteClient::Converters::HmlToPnoteConverter

Attributes

delegate[RW]

Public Class Methods

new(hml_document, show_time:, include_image_data: false) click to toggle source
# File lib/pnote_client/converters/hml_to_pnote_converter.rb, line 17
def initialize(hml_document, show_time:, include_image_data: false)
  @hml_document = hml_document
  @show_time = show_time
  @include_image_data = include_image_data

  @current_chapter = nil
  @current_sub_chapter = nil
  @current_concept = nil
  @current_exercise = nil
  @current_practice = nil
  @practice_continuous = false
  @current_problem = nil
  @current_teacher_comment = nil
  @delegate = nil
end

Public Instance Methods

convert() click to toggle source
# File lib/pnote_client/converters/hml_to_pnote_converter.rb, line 33
def convert
  pnote_document = Documents::Pnote.new

  # 한 문단(Paragraph)씩 변환
  paragraph_length = @hml_document.paragraphs.length
  paragraph_reader = Documents::Hml::ParagraphReader.new(@hml_document.paragraphs)
  paragraph_reader.next_paragraph do |paragraph, prev_paragraph, is_continuous, index|
    case paragraph_type(paragraph)
    when :chapter # 구분점
      new_chapter = Documents::Pnote::Chapter.new
      new_chapter.set_title(paragraph.content)
      add_new_chapter_to_pnote(pnote_document, new_chapter)
    when :sub_chapter_title # 구분점
      new_sub_chapter = Documents::Pnote::SubChapter.new
      new_sub_chapter.set_title(paragraph.content)
      add_new_sub_chapter_to_chapter(@current_chapter, new_sub_chapter) if @current_chapter
    when :concept_title # 구분점
      new_concept = Documents::Pnote::Concept.new
      new_concept.set_title(paragraph.content)
      add_new_concept_to_sub_chapter(@current_sub_chapter, new_concept) if @current_sub_chapter
    when :concept_content
      @current_concept.add_content(paragraph.content) if @current_concept
    when :exercise_question # 구분점
      if is_continuous
        @current_exercise.add_question_line(paragraph.content) if @current_exercise
      else
        new_exercise = Documents::Pnote::Problem.new
        new_exercise.question = paragraph.content
        add_new_exercise_to_sub_chapter(@current_sub_chapter, new_exercise) if @current_sub_chapter
      end
    when :exercise_continuous_question
      @current_exercise.add_question_line(paragraph.content) if @current_exercise
    when :exercise_answer
      @current_exercise.add_answer_line(paragraph.content) if @current_exercise
    when :exercise_explaination
      @current_exercise.add_explaination_line(paragraph.content) if @current_exercise
    when :practice_question # 구분점
      if @practice_continuous
        @current_practice.add_question_line(paragraph.content) if @current_practice
      else
        new_practice = Documents::Pnote::Problem.new
        new_practice.question = paragraph.content

        if @current_sub_chapter
          # 실전문제를 소단원에 추가
          add_new_practice_to_sub_chapter(@current_sub_chapter, new_practice) if @current_sub_chapter
        elsif @current_chapter
          # 소단원이 없다면 실전문제를 단원에 추가(심화문제)
          add_new_practice_to_chapter(@current_chapter, new_practice) if @current_chapter
        end
      end

      # 답변(미주)
      endnote_paragraph_reader = Documents::Hml::ParagraphReader.new(paragraph.endnote_paragraphs)
      endnote_paragraph_reader.next_paragraph do |endnote_paragraph, prev_endnote_paragraph, is_continuous|
        case paragraph_type(endnote_paragraph)
        when :practice_answer
          @current_practice.add_answer_line(endnote_paragraph.content) if @current_practice
        when :practice_explaination
          @current_practice.add_explaination_line(endnote_paragraph.content) if @current_practice
        when :tip_content
          @current_practice.add_tip_line(endnote_paragraph.content) if @current_practice
        end
      end

      # 과외노트(각주)
      footnote_paragraph_reader = Documents::Hml::ParagraphReader.new(paragraph.footnote_paragraphs)
      tc_index = 0
      footnote_paragraph_reader.next_paragraph do |footnote_paragraph, prev_footnote_paragraph, is_continuous|
        case paragraph_type(footnote_paragraph)
        when :practice_teacher_comment
          if is_continuous
            @current_teacher_comment.add_content_line(footnote_paragraph.content) if @current_teacher_comment
          else
            new_teacher_comment = Documents::Pnote::TeacherComment.new(show_time: (tc_index + 1) * @show_time)
            new_teacher_comment.content = footnote_paragraph.content
            add_new_teacher_comment_to_practice(@current_practice, new_teacher_comment) if @current_practice
            tc_index += 1
          end
        end
      end

      @practice_continuous = paragraph.footnote_paragraphs.size == 0 # 각주가 없다면 다음 paragraph도 같은 문제로 인식
    when :practice_continuous_question
      @current_practice.add_question_line(paragraph.content) if @current_practice
    when :choice
      @current_problem.add_choice_line(paragraph.content) if @current_problem
    end

    @delegate.paragraph_converted(count: paragraph_length, current: index) if @delegate
  end

  # 바이너리(이미지) 변환
  binary_count = @hml_document.bin_items.length
  @hml_document.bin_items.each_with_index do |bin_item, index|
    raw_data = @include_image_data ? bin_item.raw_data : nil
    pnote_document.add_image(
      Documents::Pnote::Image.new(bin_item.id, bin_item.format, bin_item.size, raw_data)
    )

    @delegate.binary_converted(count: binary_count, current: index) if @delegate
  end

  return pnote_document
end

Private Instance Methods

add_new_chapter_to_pnote(pnote_document, new_chapter) click to toggle source
# File lib/pnote_client/converters/hml_to_pnote_converter.rb, line 141
def add_new_chapter_to_pnote(pnote_document, new_chapter)
  pnote_document.add_chapter(new_chapter)
  @current_chapter = new_chapter
  @current_sub_chapter = nil
  @current_concept = nil
  @current_exercise = nil
  @current_practice = nil
  @current_problem = nil
  @current_teacher_comment = nil
end
add_new_concept_to_sub_chapter(sub_chapter, new_concept) click to toggle source
# File lib/pnote_client/converters/hml_to_pnote_converter.rb, line 162
def add_new_concept_to_sub_chapter(sub_chapter, new_concept)
  sub_chapter.add_concept(new_concept)
  @current_concept = new_concept
end
add_new_exercise_to_sub_chapter(sub_chapter, new_exercise) click to toggle source
# File lib/pnote_client/converters/hml_to_pnote_converter.rb, line 167
def add_new_exercise_to_sub_chapter(sub_chapter, new_exercise)
  sub_chapter.add_exercise(new_exercise)
  @current_exercise = new_exercise
  @current_problem = new_exercise
end
add_new_practice_to_chapter(chapter, new_practice) click to toggle source
# File lib/pnote_client/converters/hml_to_pnote_converter.rb, line 179
def add_new_practice_to_chapter(chapter, new_practice)
  chapter.add_practice(new_practice)
  @current_practice = new_practice
  @current_problem = new_practice
end
add_new_practice_to_sub_chapter(sub_chapter, new_practice) click to toggle source
# File lib/pnote_client/converters/hml_to_pnote_converter.rb, line 173
def add_new_practice_to_sub_chapter(sub_chapter, new_practice)
  sub_chapter.add_practice(new_practice)
  @current_practice = new_practice
  @current_problem = new_practice
end
add_new_sub_chapter_to_chapter(chapter, new_sub_chapter) click to toggle source
# File lib/pnote_client/converters/hml_to_pnote_converter.rb, line 152
def add_new_sub_chapter_to_chapter(chapter, new_sub_chapter)
  chapter.add_sub_chapter(new_sub_chapter)
  @current_sub_chapter = new_sub_chapter
  @current_concept = nil
  @current_exercise = nil
  @current_practice = nil
  @current_problem = nil
  @current_teacher_comment = nil
end
add_new_teacher_comment_to_practice(practice, new_teacher_comment) click to toggle source
# File lib/pnote_client/converters/hml_to_pnote_converter.rb, line 185
def add_new_teacher_comment_to_practice(practice, new_teacher_comment)
  practice.add_teacher_comment(new_teacher_comment)
  @current_teacher_comment = new_teacher_comment
end
find_style(style_id) click to toggle source
# File lib/pnote_client/converters/hml_to_pnote_converter.rb, line 197
def find_style(style_id)
  return @hml_document.styles.find {|style| style.id == style_id}
end
paragraph_type(paragraph) click to toggle source
# File lib/pnote_client/converters/hml_to_pnote_converter.rb, line 190
def paragraph_type(paragraph)
  pg_style = find_style(paragraph.style_id)
  result = ::PnoteClient::STYLES.find {|k, v| v == pg_style.name || (v.is_a?(Array) && v.include?(pg_style.name))}
  return nil if result.nil?
  return result[0]
end