Guest User

Untitled

a guest
Nov 28th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. #!/Users/yuki/.rbenv/versions/1.9.3-p194/bin/ruby
  2. # coding: utf-8
  3. # %%%{CotEditorXInput=AllText}%%%
  4.  
  5. require 'xmlrpc/client'
  6. require 'redcarpet'
  7. require 'base64'
  8.  
  9. class MyHTMLRender < Redcarpet::Render::HTML
  10. def block_code(code, language)
  11. lang = " lang-#{language}" if language
  12. "<pre class='prettyprint#{lang}'><code>#{code}</code></pre>"
  13. end
  14.  
  15. def image(link, title, alt_text)
  16. open(link, "rb") do |file|
  17. if title
  18. title = title + File.extname(link)
  19. else
  20. title = File.basename(link) unless title
  21. end
  22.  
  23. extension = File.extname(link)
  24. type = ""
  25. if /jpg|jpeg/ =~ extension
  26. type = "image/jpeg"
  27. elsif /gif/ =~ extension
  28. type = "image/gif"
  29. elsif /png/ =~ extension
  30. type = "image/png"
  31. end
  32. struct = {
  33. :name => title,
  34. :type => type,
  35. :bits => XMLRPC::Base64.new(file.read)
  36. }
  37. img = $server.call("wp.uploadFile",1,$user,$pass,struct)
  38. '<img src="' +img['url']+ '" />'
  39. end
  40. end
  41. end
  42.  
  43. def read_header
  44. while line = STDIN.gets.force_encoding('utf-8')
  45. if /#\s*(.+?)$/ =~ line
  46. @title = $1
  47. elsif /(http.+?)$/ =~ line
  48. custom_field = {'key' => "linked_list_url", 'value' => $1}
  49. @fields << custom_field
  50. @categories << "Links"
  51. elsif /^-+$/ =~ line.chomp()
  52. break
  53. else
  54. @tags = @tags + line.split(/\s*,\s*/)
  55. end
  56. end
  57. end
  58.  
  59. $user = USERNAME
  60. $pass = PASSWORD
  61. $server = XMLRPC::Client.new(DOMAIN, '/xmlrpc.php')
  62.  
  63. @title = ""
  64. @fields = []
  65. @tags = []
  66. @categories = []
  67.  
  68. read_header
  69.  
  70. options = {:fenced_code_blocks => true,
  71. :space_after_headers => true,
  72. :no_intra_emphasis => true,
  73. :autolink => true}
  74. markdown = Redcarpet::Markdown.new(MyHTMLRender, options)
  75. content = markdown.render(STDIN.read.force_encoding('utf-8'))
  76.  
  77. struct = {
  78. 'title' => @title,
  79. 'categories' => @categories,
  80. 'mt_keywords' => @tags,
  81. 'description' => content,
  82. 'custom_fields' => @fields
  83. }
  84.  
  85. id = $server.call("metaWeblog.newPost",1,$user,$pass,struct,1)
Add Comment
Please, Sign In to add comment