faq.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. require 'yaml'
  2. require 'redcloth'
  3. def process_faq_list( faqs )
  4. puts "<ul>"
  5. faqs.each do |faq|
  6. process_faq_list_item faq
  7. end
  8. puts "</ul>"
  9. end
  10. def process_faq_list_item( faq )
  11. question = faq.keys.first
  12. answer = faq.values.first
  13. print "<li>"
  14. question_text = RedCloth.new(question).to_html.gsub( %r{</?p>},"" )
  15. if answer.is_a?( Array )
  16. puts question_text
  17. process_faq_list answer
  18. else
  19. print "<a href='##{question.object_id}'>#{question_text}</a>"
  20. end
  21. puts "</li>"
  22. end
  23. def process_faq_descriptions( faqs, path=nil )
  24. faqs.each do |faq|
  25. process_faq_description faq, path
  26. end
  27. end
  28. def process_faq_description( faq, path )
  29. question = faq.keys.first
  30. path = ( path ? path + " " : "" ) + question
  31. answer = faq.values.first
  32. if answer.is_a?( Array )
  33. process_faq_descriptions( answer, path )
  34. else
  35. title = RedCloth.new( path ).to_html.gsub( %r{</?p>}, "" )
  36. answer = RedCloth.new( answer || "" )
  37. puts "<a name='#{question.object_id}'></a>"
  38. puts "<div class='faq-title'>#{title}</div>"
  39. puts "<div class='faq-answer'>#{add_api_links(answer.to_html)}</div>"
  40. end
  41. end
  42. API_OBJECTS = [ "Database", "Statement", "ResultSet",
  43. "ParsedStatement", "Pragmas", "Translator" ].inject( "(" ) { |acc,name|
  44. acc << "|" if acc.length > 1
  45. acc << name
  46. acc
  47. } + ")"
  48. def add_api_links( text )
  49. text.gsub( /#{API_OBJECTS}(#(\w+))?/ ) do
  50. disp_obj = obj = $1
  51. case obj
  52. when "Pragmas"; disp_obj = "Database"
  53. end
  54. method = $3
  55. s = "<a href='http://sqlite-ruby.rubyforge.org/classes/SQLite/#{obj}.html'>#{disp_obj}"
  56. s << "##{method}" if method
  57. s << "</a>"
  58. s
  59. end
  60. end
  61. faqs = YAML.load( File.read( "faq.yml" ) )
  62. puts <<-EOF
  63. <html>
  64. <head>
  65. <title>SQLite3/Ruby FAQ</title>
  66. <style type="text/css">
  67. a, a:visited, a:active {
  68. color: #00F;
  69. text-decoration: none;
  70. }
  71. a:hover {
  72. text-decoration: underline;
  73. }
  74. .faq-list {
  75. color: #000;
  76. font-family: vera-sans, verdana, arial, sans-serif;
  77. }
  78. .faq-title {
  79. background: #007;
  80. color: #FFF;
  81. font-family: vera-sans, verdana, arial, sans-serif;
  82. padding-left: 1em;
  83. padding-top: 0.5em;
  84. padding-bottom: 0.5em;
  85. font-weight: bold;
  86. font-size: large;
  87. border: 1px solid #000;
  88. }
  89. .faq-answer {
  90. margin-left: 1em;
  91. color: #000;
  92. font-family: vera-sans, verdana, arial, sans-serif;
  93. }
  94. .faq-answer pre {
  95. margin-left: 1em;
  96. color: #000;
  97. background: #FFE;
  98. font-size: normal;
  99. border: 1px dotted #CCC;
  100. padding: 1em;
  101. }
  102. h1 {
  103. background: #005;
  104. color: #FFF;
  105. font-family: vera-sans, verdana, arial, sans-serif;
  106. padding-left: 1em;
  107. padding-top: 1em;
  108. padding-bottom: 1em;
  109. font-weight: bold;
  110. font-size: x-large;
  111. border: 1px solid #00F;
  112. }
  113. </style>
  114. </head>
  115. <body>
  116. <h1>SQLite/Ruby FAQ</h1>
  117. <div class="faq-list">
  118. EOF
  119. process_faq_list( faqs )
  120. puts "</div>"
  121. process_faq_descriptions( faqs )
  122. puts "</body></html>"