Guest User

Untitled

a guest
Feb 21st, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. class Dot
  2. def initialize
  3. @graph = [[]]
  4. end
  5.  
  6. def __dot_graph &block
  7. instance_eval &block
  8. end
  9.  
  10. # TODO: make this work
  11. def self.const_missing name, *params, &block
  12. method_missing(name, *params, &block)
  13. end
  14.  
  15. def method_missing name, *params, &block
  16. unless (@graph.last[-2,2].any?{|e| [:>, :<].include? e} rescue true)
  17. tmp = @graph.last
  18. @graph.delete tmp
  19. new = tmp.last
  20. tmp = tmp[0..-2]
  21. @graph << tmp
  22. @graph << [new]
  23. end
  24. @graph.last << name
  25. return self
  26. end
  27.  
  28. def > *params
  29. @graph.last.insert(-2, :>)
  30. return self
  31. end
  32.  
  33. def < *params
  34. @graph.last.insert(-2, :>)
  35. return self
  36. end
  37.  
  38. def __dot_export file = 'out.dot'
  39. File.open(file, 'w+') do |f|
  40. __dot_to_s.split("\n").each do |line|
  41. f.puts line
  42. end
  43. end
  44. end
  45.  
  46. def __dot_import file
  47. File.readlines(file).each do |line|
  48. instance_eval line
  49. end
  50. end
  51.  
  52. def __dot_to_s
  53. p @graph
  54. s = %{
  55. digraph A {
  56. #{@graph.map{|s| s.join(' ').gsub('>', '->')}.join(";\n ")}
  57. }
  58. }
  59. puts s
  60. s
  61. end
  62. end
  63.  
  64. ARGV.each do |file|
  65. base = File.basename(file, '.rdot')
  66. dot = Dot.new
  67. dot.__dot_import file
  68. dot.__dot_export "#{base}.dot"
  69. out = `dot #{base}.dot -Tps`
  70. File.open("#{base}.ps", 'w+') do |file|
  71. out.each_line do |line|
  72. file.puts line
  73. end
  74. end
  75.  
  76. system('ps2pdf', "#{base}.ps", "#{base}.pdf")
  77. end
Add Comment
Please, Sign In to add comment