Class: Yast::RichTextClass

Inherits:
Module
  • Object
show all
Defined in:
library/types/src/modules/RichText.rb

Instance Method Summary collapse

Instance Method Details

#DetectRichText(file) ⇒ Symbol

Parse provided text and see if it contains richtext



121
122
123
124
125
126
127
128
129
# File 'library/types/src/modules/RichText.rb', line 121

def DetectRichText(file)
  return :error unless Ops.greater_than(SCR.Read(path(".target.size"), file), 0)

  text = Convert.to_string(SCR.Read(path(".target.string"), file))

  return :empty if text == ""

  Builtins.regexpmatch(text, "</.*>") ? :richtext : :plaintext
end

#DropWS(text) ⇒ Object



41
42
43
44
# File 'library/types/src/modules/RichText.rb', line 41

def DropWS(text)
  filteredlist = Builtins.splitstring(text, "\n\t")
  String.CutBlanks(Builtins.mergestring(filteredlist, " "))
end

#mainObject



36
37
38
39
# File 'library/types/src/modules/RichText.rb', line 36

def main
  textdomain "base"
  Yast.import "String"
end

#Rich2Plain(richtext) ⇒ Object

Convert a richtext string into a formatted plain text.



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
# File 'library/types/src/modules/RichText.rb', line 49

def Rich2Plain(richtext)
  Builtins.y2debug("richtext=%1", richtext)

  lparts = Builtins.splitstring(DropWS(richtext), "<")
  Builtins.y2debug("lparts=%1", lparts)

  # Am I in <LI>?
  inli = false

  # Indentation level
  indents = 0

  result = ""
  Builtins.foreach(lparts) do |lpart|
    s = Builtins.find(lpart, ">")
    tag = Builtins.tolower(Builtins.substring(lpart, 0, s))
    # *** Handle tags ****

    # BR
    if tag == "br"
      result = Ops.add(result, "\n")
    # P
    elsif tag == "p"
      result = Ops.add(result, "\n")
    # UL
    elsif tag == "ul"
      inli = true
      indents = Ops.add(indents, 1)
    # /UL
    elsif tag == "/ul"
      result = Ops.add(result, "\n") if inli && indents == 1
      indents = Ops.subtract(indents, 1)
      inli = false
    # LI
    elsif tag == "li"
      result = Ops.add(result, "\n") if inli
      inli = true
    # /LI
    elsif tag == "/li"
      inli = false
      result = Ops.add(result, "\n")
    end
    # *** Add the text ****
    if s != -1
      lpart = String.CutBlanks(Builtins.substring(lpart, Ops.add(s, 1)))
    end
    next if Builtins.regexpmatch(lpart, "^[ \n\t]*$")
    next if lpart == "&nbsp;"
    if lpart != "" && inli
      i = 1
      while Ops.less_than(i, indents)
        result = Ops.add(result, "  ")
        i = Ops.add(i, 1)
      end
      lpart = Ops.add("* ", lpart)
    end
    # result = result + "[" + lpart + "]";
    result = Ops.add(result, lpart)
  end
  result = String.CutBlanks(result)
  if Ops.greater_than(Builtins.size(result), 0) &&
      Builtins.substring(result, Ops.subtract(Builtins.size(result), 1)) != "\n"
    result = Ops.add(result, "\n")
  end

  Builtins.y2debug(result)
  result
end