Class: PnoteClient::Converters::HmlToPnoteConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/pnote_client/converters/hml_to_pnote_converter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hml_document, show_time:, type_style_mapper: {}, include_image_data: false) ⇒ HmlToPnoteConverter

Returns a new instance of HmlToPnoteConverter.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pnote_client/converters/hml_to_pnote_converter.rb', line 16

def initialize(hml_document, show_time:, type_style_mapper: {}, include_image_data: false)
  @hml_document = hml_document
  @show_time = show_time
  @type_style_mapper = type_style_mapper
  @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

Instance Attribute Details

#delegateObject

Returns the value of attribute delegate.



14
15
16
# File 'lib/pnote_client/converters/hml_to_pnote_converter.rb', line 14

def delegate
  @delegate
end

Instance Method Details

#convertObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# 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)
      footnote_paragraph_reader.next_paragraph do |footnote_paragraph, prev_footnote_paragraph, is_continuous, index|
        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: (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
          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