Class: VivlioStarter::CLI::IndexCommands::TermLine

Inherits:
Data
  • Object
show all
Defined in:
lib/vivlio_starter/cli/index/term_line.rb

Overview

レビューファイルの用語行

Constant Summary collapse

LINE =

用語行の綴り。[フラグ]『NEW! ラベル』**用語** (読み) と行末。 ここを変えると 9 つの読み取りすべてに効く——だからこそ 1 箇所に置く。

/^- \[([^\]]*)\](?: `(NEW!|Today)`)? \*\*(.+?)\*\* \(([^)]+)\)(.*)$/
FLAG_AND_MAIN =

フラグ本体と主要参照を分ける。igm?21,22 → ['ig', ['21','22'], true]

/\A([^m]*)m(\?)?(.*)\z/
INDEX_FLAGS =
%w[i ig gi x].freeze
GLOSSARY_FLAGS =
%w[g ig gi].freeze
REJECT_BOTH_FLAGS =
%w[r -ig -gi].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#flagsObject (readonly)

Returns the value of attribute flags

Returns:

  • (Object)

    the current value of flags



38
39
40
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 38

def flags
  @flags
end

#labelObject (readonly)

Returns the value of attribute label

Returns:

  • (Object)

    the current value of label



38
39
40
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 38

def label
  @label
end

#mainObject (readonly)

Returns the value of attribute main

Returns:

  • (Object)

    the current value of main



38
39
40
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 38

def main
  @main
end

#suggestedObject (readonly)

Returns the value of attribute suggested

Returns:

  • (Object)

    the current value of suggested



38
39
40
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 38

def suggested
  @suggested
end

#termObject (readonly)

Returns the value of attribute term

Returns:

  • (Object)

    the current value of term



38
39
40
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 38

def term
  @term
end

#trailerObject (readonly)

Returns the value of attribute trailer

Returns:

  • (Object)

    the current value of trailer



38
39
40
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 38

def trailer
  @trailer
end

#yomiObject (readonly)

Returns the value of attribute yomi

Returns:

  • (Object)

    the current value of yomi



38
39
40
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 38

def yomi
  @yomi
end

Class Method Details

.build(flags, main: [], suggested: false) ⇒ Object

フラグ欄を組み立てる。主要参照は章番号だけのときフラグへ収める ——[igm21#Markdown とは] は読みにくいので、そういう値は子行に譲る。



66
67
68
69
70
71
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 66

def build(flags, main: [], suggested: false)
  tokens = Array(main)
  return "[#{flags}]" unless in_flag?(tokens)

  "[#{flags}m#{suggested ? '?' : ''}#{tokens.join(',')}]"
end

.in_flag?(tokens) ⇒ Boolean

フラグ欄へ収めてよい値か(章番号だけか)

Returns:

  • (Boolean)


74
75
76
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 74

def in_flag?(tokens)
  tokens.any? && tokens.all? { it.to_s.match?(/\A\d+\z/) }
end

.parse(line) ⇒ Object

1 行を解釈する。用語行でなければ nil



52
53
54
55
56
57
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 52

def parse(line)
  m = LINE.match(line) or return nil

  flags, main, suggested = split_flags(m[1])
  new(flags:, main:, suggested:, term: m[3], yomi: m[4], label: m[2], trailer: m[5].to_s)
end

.scan(content) ⇒ Object

文字列に含まれる用語行をすべて拾う



60
61
62
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 60

def scan(content)
  content.to_s.lines.filter_map { parse(it) }
end

Instance Method Details

#glossary?Boolean

Returns:

  • (Boolean)


89
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 89

def glossary? = GLOSSARY_FLAGS.include?(flags)

#index?Boolean

Returns:

  • (Boolean)


88
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 88

def index? = INDEX_FLAGS.include?(flags)

#pending?Boolean

保留([ ] や空欄)

Returns:

  • (Boolean)


98
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 98

def pending? = flags.strip.empty?

#reject_both?Boolean

Returns:

  • (Boolean)


90
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 90

def reject_both? = REJECT_BOTH_FLAGS.include?(flags)

#reject_glossary?Boolean

Returns:

  • (Boolean)


92
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 92

def reject_glossary? = flags == '-g'

#reject_index?Boolean

Returns:

  • (Boolean)


91
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 91

def reject_index? = flags == '-i'

#scoreObject

行末から拾えるスコア



101
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 101

def score = trailer[/- スコア:\s*([\d.]+)/, 1]&.to_f

#unrejecting?Boolean

除外済みリストから拾い上げる対象(セクション 4 で復帰マークが付いた行)

Returns:

  • (Boolean)


95
# File 'lib/vivlio_starter/cli/index/term_line.rb', line 95

def unrejecting? = (INDEX_FLAGS + GLOSSARY_FLAGS).include?(flags)