Class: MyMediaPages

Inherits:
MyMedia::Base
  • Object
show all
Includes:
MyMedia::IndexReader, PageReader
Defined in:
lib/mymedia-pages.rb

Instance Method Summary collapse

Methods included from PageReader

#read, #view

Constructor Details

#initialize(media_type: media_type='pages', public_type: @public_type=media_type, ext: '.(html|md|txt)', config: nil, log: nil, debug: false) ⇒ MyMediaPages

Returns a new instance of MyMediaPages.

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mymedia-pages.rb', line 52

def initialize(media_type: media_type='pages',
     public_type: @public_type=media_type, ext: '.(html|md|txt)',
               config: nil, log: nil, debug: false)
  
  raise MyMediaPagesError, 'Missing config' unless config
  
  super(media_type: media_type, public_type: @public_type=media_type,
                          ext: '.(html|md|txt)', config: config, log: log)

  @target_ext = '.html'
  @static_html = true
  @debug = debug
  
end

Instance Method Details

#copy_publish(filename, raw_msg = '') ⇒ Object



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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/mymedia-pages.rb', line 67

def copy_publish(filename, raw_msg='')

  @log.info 'MyMediaPagesinside copy_publish' if @log
  @filename = filename
  #jr2022-10-09 src_path = File.join(@media_src, filename)
  src_path = filename

  if File.basename(src_path)[/[a-z]\d{6}T\d{4}\.(?:html)/] then      
    return file_publish(src_path, raw_msg)
  end

  file_publish(src_path, raw_msg) do |destination, raw_destination|

    ext = File.extname(src_path)
    
    if ext[/\.(?:md|txt)/] then      

      raw_dest_xml = raw_destination.sub(/html$/,'xml')
      dest_xml = destination.sub(/html$/,'xml')
      x_destination = raw_destination.sub(/\.html$/,ext)

      puts "src: %s dest: %s" % [src_path, x_destination] if @debug
      FileX.cp src_path, x_destination
      
      source = x_destination[/\/r\/#{@public_type}.*/]
      s = @website + source

      relative_path = s[/https?:\/\/[^\/]+([^$]+)/,1]
      src_content = FileX.read src_path
      doc = xml(src_content, relative_path, filename)

      return unless doc

      modify_xml(doc, raw_dest_xml)
      modify_xml(doc, dest_xml)

      @log.info 'mymedia_pages/copy_publish: after modify_xml' if @log

      FileX.write raw_destination,
          xsltproc(File.join(@home, @www, 'r', 'xsl', @public_type + '.xsl'),
                   raw_dest_xml)
      FileX.write destination,
          xsltproc(File.join(@home, @www, 'xsl', @public_type + '.xsl'),
                   dest_xml)

      html_filename = basename(@media_src, src_path).sub(/(?:md|txt)$/,'html')
      
      
      xml_filename = html_filename.sub(/html$/,'xml')

      FileX.mkdir_p File.dirname(File.join(File.dirname(destination),
                                           html_filename))
      FileX.cp destination, File.join(File.dirname(destination),
                                      html_filename)

      FileX.mkdir_p File.dirname( File.join(File.dirname(dest_xml),
                                            xml_filename))
      FileX.cp dest_xml, File.join(File.dirname(dest_xml), xml_filename)

      tags = doc.root.xpath('summary/tags/tag/text()')
      raw_msg = "%s %s" % [doc.root.text('summary/title'), 
              tags.map {|x| "#%s" % x }.join(' ')]
      
      
      @log.info "msg: %s tags: %s" % [raw_msg, tags.inspect] if @log


    else
      
      html_filename = basename(src_path)
      
      if html_filename =~ /\// then
        FileX.mkdir_p File.dirname(html_filename)
      end        
      
      FileX.cp src_path, destination
      FileX.cp src_path, raw_destination
      
      raw_msg = FileX.read(destination)[/<title>([^<]+)<\/title>/,1]
    end
          
    if not File.basename(src_path)[/[a-z]\d{6}T\d{4}\.(?:html|md|txt)/] then
      
      @log.info 'MyMediaPages::copy_publish before FileUtils' if @log

      html_filepath = File.join(@home, @www, @public_type, html_filename)
      FileX.mkdir_p File.dirname(html_filepath)
      FileX.cp destination, html_filepath

      if xml_filename then
        xml_filepath = File.join(@home, @www, @public_type,  xml_filename)
        FileUtils.cp dest_xml, xml_filepath
      end

      static_filepath = File.join(@home, @www, @public_type, 'static.xml')
      x_filename = @static_html == true ? html_filename : xml_filename        
      
      if @log then
        @log.info 'MyMediaPages::copy_publish ->file_publish ' +
            'before publish_dynarex'
      end
      
      target_url = File.join(@website,  @public_type, x_filename)
      target_url.sub!(/\.html$/,'') if @omit_html_ext
      publish_dynarex(static_filepath, {title: raw_msg, url: target_url })                  

    end

    [raw_msg, target_url]
  end    

end

#escape(s) ⇒ Object



180
181
182
# File 'lib/mymedia-pages.rb', line 180

def escape(s)
  s.gsub(/ +/,'_')#.gsub(/'/,'%27')
end

#writecopy_publish(raws, filename = nil) ⇒ Object



184
185
186
187
188
189
190
191
192
193
# File 'lib/mymedia-pages.rb', line 184

def writecopy_publish(raws, filename=nil)

  s = raws.strip.gsub(/\r/,'')

  title = escape(s.lines[0].chomp)
  filename ||= title + '.txt'
  FileX.write File.join(@media_src, filename), s

  copy_publish File.join(@media_src, filename)
end