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



111
112
113
114
115
116
117
118
119
# File 'library/types/src/modules/RichText.rb', line 111

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



39
40
41
42
# File 'library/types/src/modules/RichText.rb', line 39

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

#mainObject



34
35
36
37
# File 'library/types/src/modules/RichText.rb', line 34

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

#Rich2Plain(richtext) ⇒ Object

Convert a richtext string into a formatted plain text.



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

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 ****

    case tag
    when "br", "p"
      result = Ops.add(result, "\n")
    when "ul"
      inli = true
      indents = Ops.add(indents, 1)
    when "/ul"
      result = Ops.add(result, "\n") if inli && indents == 1
      indents = Ops.subtract(indents, 1)
      inli = false
    when "li"
      result = Ops.add(result, "\n") if inli
      inli = true
    when "/li"
      inli = false
      result = Ops.add(result, "\n")
    end
    # *** Add the text ****
    lpart = String.CutBlanks(Builtins.substring(lpart, Ops.add(s, 1))) if s != -1
    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