Class: FileHandler
- Inherits:
-
Object
- Object
- FileHandler
- Defined in:
- lib/phari_doc_gen/fileHandler.rb
Instance Method Summary collapse
- #clear(str) ⇒ Object
-
#dataValue(argument, objects) ⇒ Object
If the clause is a param or return value, decode the name and datatype.
-
#decodeArgument(line) ⇒ Object
Decode which clause is on a given line, and which arguments.
-
#decodeDescription(line) ⇒ Object
Decode the text from a description clause.
- #gem_libdir ⇒ Object
-
#hasBetween?(line, stringA, stringB) ⇒ Boolean
Verify if line contains markdown syntax like bold.
-
#hasDescription?(line, lineIndex, fileName) ⇒ Boolean
The ** description clause has his own method seen that it has different syntax.
-
#hasKeyword?(line, lineIndex, fileName) ⇒ Boolean
Verify if a given line has any clause.
- #hasSomething?(line) ⇒ Boolean
-
#higlightText(line) ⇒ Object
Convert bold markdown to bold HTML.
-
#italicText(line) ⇒ Object
Convert italic markdown to italic HTML.
-
#linkText(line) ⇒ Object
Convert [www.someaddress.com](text) markdown to <a href=“www.someaddress.com”>text</a> HTML.
- #nameFormat(name) ⇒ Object
-
#packageExistis?(packageName) ⇒ Boolean
Reading files Find a folder with the project name.
-
#readFiles(path) ⇒ Object
Read models, helpers and controllers.
-
#readMethods(helper, helpername) ⇒ Object
Read all methods from a helper file; helper = file, helpername = file path.
-
#readProject(path) ⇒ Object
Read the ‘README.md’ file and generate descriprion.
-
#readProjectDescription(readme) ⇒ Object
Generate project descriprion based on ‘README.md’.
-
#readRelations(modelFile, modelname, modelPath) ⇒ Object
Read relations from model file.
-
#readRoutes(controller, controllername) ⇒ Object
Read all routes from a controller file; controller = file, controllername = file path.
-
#writeCSS(file) ⇒ Object
Write the css content.
-
#writeFiles(models, projectName, projectDescription, path) ⇒ Object
File writing.
-
#writeModelPage(file, model, project) ⇒ Object
Write a model HTML page content.
-
#writeProjectPage(project, file, models, description) ⇒ Object
Write the main project HTML content.
Instance Method Details
#clear(str) ⇒ Object
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 459 def clear str firstIndex = 0 lastIndex = str.length for i in 0..str.length-1 if str[i].match(/^[[:alpha:]]$/) lastIndex = i + 1 end end str = str.slice(0, lastIndex) for i in 0..str.length-1 if str[i].match(/^[[:alpha:]]$/) firstIndex = i - 1 break end end str = str.slice(firstIndex + 1, str.length - firstIndex) str end |
#dataValue(argument, objects) ⇒ Object
If the clause is a param or return value, decode the name and datatype
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 479 def dataValue(argument, objects) args = [] while argument.include? ',' raise ArgumentError, 'Unexpected ","' unless hasSomething?(argument) args << argument.slice(0, argument.index(',')) argument = argument.slice(argument.index(',') + 1, argument.length - argument.index(',')) argument = clear(argument) end argument = clear(argument) args << argument args.each do |arg| if arg.include? "\s" notNull = true isObject = false isCollection = false isChild = false name = arg.slice(0, arg.index("\s")) if name.end_with?('?') notNull = false name = name.slice(0, name.index('?')) end type = arg.slice(arg.index("\s") + 1, arg.size - 2) if type.include? "\s" example = type.slice(type.index("\s")+1, type.size-1-type.index("\s")) type = type.slice(0, type.index("\s")) end type = type.slice(0, type.size-1) if type.end_with?("\s") || type.end_with?("\n") isObject = type.downcase == "object" isCollection = type.downcase == "array" # Inserting attributes inside objects if name.include?(">") isChild = true path = [] while name.include?(">") prefix = name.slice(0, name.index(">")) name = name.slice(name.index(">")+1, name.size-(name.index(">")+1)) path << prefix end fatherObject = nil children = objects path.each do |object| if (fatherObject.nil? || fatherObject.isObject) raise ArgumentError, "Object #{object} does not exists!" unless children.has_key?(object) && (children["#{object}"].isObject || children["#{object}"].isCollection) fatherObject = children["#{object}"] elsif (fatherObject.isCollection) raise ArgumentError, "Object #{object} is not a position of #{fatherObject.name}!" unless (children["content"].isObject || children["content"].isCollection) fatherObject = children["content"] end children = fatherObject.childAttributes end end data = MethodParam.new(name, type, notNull, isObject, isCollection, isChild, example) fatherObject.addField data unless (fatherObject.nil? || fatherObject.isCollection) fatherObject.setContent data unless (fatherObject.nil? || fatherObject.isObject) # Return the data objects["#{data.name}"] = data unless data.isChild elsif arg.include? "nil" data = MethodParam.new('', 'nil', false, false, false, false, nil) objects["#{data.name}"] = data unless data.isChild else # If any information is missing or incorrect, raises an Argument Error raise ArgumentError.new("Syntax error: #{arg} is no data") end end end |
#decodeArgument(line) ⇒ Object
Decode which clause is on a given line, and which arguments
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 432 def decodeArgument(line) args = [] initialIndex = line.index('@') + 1 sentence = line.slice(initialIndex, line.length - initialIndex) # Raises an Argument Error if some error is found raise ArgumentError.new("Sytanx error: expected 2 arguments (none given)") unless hasSomething?(sentence) raise ArgumentError.new("Sytanx error: expected 2 arguments (one given)") unless sentence.include?("\s") finalIndex = line.index("\s", line.index('@')) length = finalIndex - initialIndex keyword = line.slice(initialIndex, length) argument = line.slice(finalIndex + 1, line.size - finalIndex) raise ArgumentError.new("Sytanx error: expected 2 arguments (one given)") unless hasSomething?(argument) args << keyword args << argument # Return array with clause and arguments args end |
#decodeDescription(line) ⇒ Object
Decode the text from a description clause
546 547 548 549 550 551 552 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 546 def decodeDescription(line) initialIndex = line.index('*') + 3 length = line.size - initialIndex description = line.slice(initialIndex, length) # Return the description text description end |
#gem_libdir ⇒ Object
619 620 621 622 623 624 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 619 def gem_libdir t = ["#{File.dirname(File.($0))}/../lib/#{Meta::NAME}", "#{Gem.dir}/gems/#{Meta::NAME}-#{Meta::VERSION}/lib/#{Meta::NAME}"] t.each {|i| return i if File.readable?(i) } raise "both paths are invalid: #{t}" end |
#hasBetween?(line, stringA, stringB) ⇒ Boolean
Verify if line contains markdown syntax like bold
141 142 143 144 145 146 147 148 149 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 141 def hasBetween?(line, stringA, stringB) if line.include?(stringA) initial = line.index(stringA) len = line.length - initial lineSlice = line.slice(initial, len) return true if lineSlice.include?(stringB) end false end |
#hasDescription?(line, lineIndex, fileName) ⇒ Boolean
The ** description clause has his own method seen that it has different syntax
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 408 def hasDescription?(line, lineIndex, fileName) if (line.include? '#') && (line.include? '**') for i in 0..line.index('#')-1 if line[i].match(/^[[:alpha:]]$/) return false end end for i in line.index('#')+1..line.index('**')-1 if line[i].match(/^[[:alpha:]]$/) lineText = line.slice(line.index('#'), line.index("\n") - line.index('#')) # A warning informing file and line is sent if a possible error is found puts "Warning: in #{fileName}:#{lineIndex}: #{lineText} is not a description" return false end end # Return true if it has description clause true else # And false if it don't false end end |
#hasKeyword?(line, lineIndex, fileName) ⇒ Boolean
Verify if a given line has any clause
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 384 def hasKeyword?(line, lineIndex, fileName) if (line.include? '#') && (line.include? '@') for i in 0..line.index('#')-1 if line[i].match(/^[[:alpha:]]$/) return false end end for i in line.index('#')+1..line.index('@')-1 if line[i].match(/^[[:alpha:]]$/) lineText = line.slice(line.index('#'), line.index("\n") - line.index('#')) # A warning informing file and line is sent if a possible error is found puts "Warning: in #{fileName}:#{lineIndex}: #{lineText} is not a keyword" return false end end # Return true if it does true else # And false if it don't false end end |
#hasSomething?(line) ⇒ Boolean
450 451 452 453 454 455 456 457 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 450 def hasSomething?(line) for i in 0..line.length-1 if line[i].match(/^[[:alpha:]]$/) return true end end false end |
#higlightText(line) ⇒ Object
Convert bold markdown to bold HTML
152 153 154 155 156 157 158 159 160 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 152 def higlightText(line) initialIndex = line.index('**') + 2 finalIndex = line.index('**', initialIndex) initialString = line.slice(0, initialIndex - 2) higlightedString = line.slice(initialIndex, finalIndex - initialIndex) finalString = line.slice(finalIndex + 2, line.length - finalIndex + 2) line = initialString + '<b>' + higlightedString + '</b>' + finalString line end |
#italicText(line) ⇒ Object
Convert italic markdown to italic HTML
163 164 165 166 167 168 169 170 171 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 163 def italicText(line) initialIndex = line.index('*') + 1 finalIndex = line.index('*', initialIndex) initialString = line.slice(0, initialIndex - 1) higlightedString = line.slice(initialIndex, finalIndex - initialIndex) finalString = line.slice(finalIndex + 1, line.length - finalIndex + 1) line = initialString + '<i>' + higlightedString + '</i>' + finalString line end |
#linkText(line) ⇒ Object
Convert [www.someaddress.com](text) markdown to <a href=“www.someaddress.com”>text</a> HTML
174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 174 def linkText(line) if line.include?(' [') && line.include?('](') initialContentIndex = line.index('[') + 1 finalContentIndex = line.index(']', initialContentIndex) initialLinkIndex = line.index('](') + 2 finalLinkIndex = line.index(')', initialLinkIndex) initialString = line.slice(0, initialContentIndex - 1) contentString = line.slice(initialContentIndex, finalContentIndex - initialContentIndex) linkString = line.slice(initialLinkIndex, finalLinkIndex - initialLinkIndex) finalString = line.slice(finalLinkIndex + 1, line.length - finalLinkIndex + 1) line = initialString + '<a href="' + linkString + '">' + contentString + '</a>' + finalString end line end |
#nameFormat(name) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 83 def nameFormat(name) name = name.capitalize name = name.slice(0, name.length-1) if name.end_with?('/') while name.include?("/") name = name.slice(name.index("/")+1, name.length - name.index("/")) name = name.capitalize end while name.include?("_") prefix = name.slice(0, name.index("_")) sulfix = name.slice(name.index("_")+1, name.length - name.index("_")) name = "#{prefix.capitalize} #{sulfix.capitalize}" end while name.include?("-") prefix = name.slice(0, name.index("-")) sulfix = name.slice(name.index("-")+1, name.length - name.index("-")) name = "#{prefix.capitalize} #{sulfix.capitalize}" end name end |
#packageExistis?(packageName) ⇒ Boolean
Reading files Find a folder with the project name
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 18 def packageExistis?(packageName) packageName += '/' unless packageName.end_with?('/') if Dir.exists?(packageName) # Return the folder path if found return packageName else # Exit if not found puts 'Aborting: package not found' exit end end |
#readFiles(path) ⇒ Object
Read models, helpers and controllers
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 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 49 def readFiles(path) models = [] # Find each model, helper and controller Dir.glob(path + 'models/*.rb') do |file| filename = file.to_s inputFile = File.new(filename) modelFile = File.new(filename, 'r') modelname = File.basename(inputFile, '.rb') helpername = path + 'app/helpers/' + modelname + '_helper.rb' controllername = path + 'app/controllers/' + modelname + '.rb' modelname = nameFormat(modelname) # Call the necessary methods for each helper, model and controller relations = readRelations(modelFile, modelname, filename) modelFile.close if File.exist?(helpername) modelHelper = File.new(helpername, 'r') methods = readMethods(modelHelper, helpername) modelHelper.close end if File.exist?(controllername) modelController = File.new(controllername, 'r') routes = readRoutes(modelController, controllername) modelController.close end currentModel = Modelo.new(modelname, methods, routes, relations) models << currentModel methods = nil routes = nil relations = nil end # Return models models end |
#readMethods(helper, helpername) ⇒ Object
Read all methods from a helper file; helper = file, helpername = file path
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 229 def readMethods(helper, helpername) lineIndex = 0 tipo = '' nome = '' descricao = '' inputs = {} outputs = {} methods = [] toSent = true arr = IO.readlines(helper) # Read each line from helper arr.each do |line| lineIndex += 1 begin # Verify if it has a clause if hasKeyword?(line, lineIndex, helpername) # If it does, identify which clause it is arg = decodeArgument(line) keyword = arg[0] argument = arg[1] # Execute clause case keyword when 'methods' if nome != '' methods << Metodo.new(nome, tipo, inputs, outputs, descricao) inputs = {} outputs = {} toSent = false end tipo = argument when 'name' if nome != '' && toSent methods << Metodo.new(nome, tipo, inputs, outputs, descricao) inputs = {} outputs = {} else toSent = true end nome = argument when 'param' data = dataValue(argument, inputs) when 'return' data = dataValue(argument, outputs) else # If a possible syntax error is found, a warning is sent informing file and linne puts "Warning: in #{helpername}:#{lineIndex}: #{keyword} is not a keyword" if keyword != 'namespace' end end descricao = decodeDescription(line) if hasDescription?(line, lineIndex, helpername) rescue ArgumentError => e # If a syntax error is found, it raises an Argument Error informing the file and line puts "Warning: #{helpername}:#{lineIndex}: in #{nome} " + e. end end methods << Metodo.new(nome, tipo, inputs, outputs, descricao) # Return a methods array methods end |
#readProject(path) ⇒ Object
Read the ‘README.md’ file and generate descriprion
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 31 def readProject(path) projectDescription = '' if File.exists?(path + 'README.md') readme = File.new(path + 'README.md') projectDescription = readProjectDescription(readme) readme.close projectDescription elsif File.exists?(path + 'readme.md') readme = File.new(path + 'readme.md') projectDescription = readProjectDescription(readme) readme.close projectDescription else puts 'Warning: No readme.md file found.' end end |
#readProjectDescription(readme) ⇒ Object
Generate project descriprion based on ‘README.md’
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 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 106 def readProjectDescription(readme) isCode = false @projectDescription = '' arr = IO.readlines(readme) # Read each line from the README arr.each do |line| # Read markdown syntax and trasform into HTML tags next if line.start_with?('# ') || line.start_with?('#!shell') line = '<h1>' + line.slice(2, line.length - 2) + '</h1>' if line.start_with?('# ') line = '<h2>' + line.slice(3, line.length - 3) + '</h2>' if line.start_with?('## ') line = '<h3>' + line.slice(4, line.length - 4) + '</h3>' if line.start_with?('### ') line = '<h4>' + line.slice(5, line.length - 5) + '</h4>' if line.start_with?('#### ') line = '<h5>' + line.slice(6, line.length - 6) + '</h5>' if line.start_with?('##### ') line = '<li>' + line.slice(2, line.length - 2) + '</li>' if line.start_with?('* ') line = higlightText(line) while hasBetween?(line, '**', '**') line = italicText(line) while hasBetween?(line, '*', '*') if line.include?('```') if isCode line = '</code><br><br>' isCode = false else line = '<br><br><code>' isCode = true end end while line.include?(' [') && line.include?('](') line = linkText(line) end @projectDescription += line end # Return the HTML formated lines @projectDescription end |
#readRelations(modelFile, modelname, modelPath) ⇒ Object
Read relations from model file
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 190 def readRelations(modelFile, modelname, modelPath) lineIndex = 0 relations = [] begin arr = IO.readlines(modelFile) # Read each line from model arr.each do |line| lineIndex += 1 # Verify presence of any clause if hasKeyword?(line, lineIndex, modelname) # If it does, identify which clause it is arg = decodeArgument(line) keyword = arg[0] argument = arg[1] # Execute clause case keyword when 'belongs_to' relation = modelname + ' belongs to ' + argument relations << relation when 'has_one' relation = modelname + ' has one ' + argument relations << relation when 'has_many' relation = modelname + ' has many ' + argument.pluralize relations << relation when 'has_and_belongs_to_many' relation = modelname + ' has and belongs to many ' + argument.pluralize relations << relation end end end rescue ArgumentError => e # If a syntax error is found, it raises an Argument Error informing the file and line puts "Warning: #{modelPath}:#{lineIndex} " + e. end relations end |
#readRoutes(controller, controllername) ⇒ Object
Read all routes from a controller file; controller = file, controllername = file path
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 289 def readRoutes(controller, controllername) lineIndex = 0 tipo = '' requisicao = '' dataInput = '' nome = '' inputs = {} outputs = {} routes = [] toSent = true arr = IO.readlines(controller) # Read each line from controller arr.each do |line| begin lineIndex += 1 # Skip to next line if no clause is found next unless hasKeyword?(line, lineIndex, controllername) # If it has any clause, identify which one it is arg = decodeArgument(line) keyword = arg[0] argument = arg[1] case keyword # Then execute clause when 'route' if nome != '' routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs) inputs = {} outputs = {} toSent = false end tipo = argument when 'POST' if nome != '' && toSent routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs) inputs = {} outputs = {} else toSent = true end requisicao = keyword dataInput = argument when 'GET' if nome != '' && toSent routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs) inputs = {} outputs = {} else toSent = true end requisicao = keyword dataInput = argument when 'PUT' if nome != '' && toSent routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs) inputs = {} outputs = {} else toSent = true end requisicao = keyword dataInput = argument when 'DELETE' if nome != '' && toSent routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs) inputs = {} outputs = {} else toSent = true end requisicao = keyword dataInput = argument when 'name' nome = argument when 'param' data = dataValue(argument, inputs) when 'return' data = dataValue(argument, outputs) else # If a possible syntax error is found, a warning is sent informing file and linne # (Here in the namespace comparison, i'm doing a little mcgyver) puts "Warning: in #{controllername}:#{lineIndex}: #{keyword} is not a keyword" if keyword != 'namespace' end rescue ArgumentError => e # If a syntax error is found, it raises an Argument Error informing the file and line puts "Warning: #{controllername}:#{lineIndex}: in #{nome} " + e. end end routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs) # Return a routes array routes end |
#writeCSS(file) ⇒ Object
Write the css content
578 579 580 581 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 578 def writeCSS(file) css_file = gem_libdir+'/templates/style.css' file.write(File.read(css_file)) end |
#writeFiles(models, projectName, projectDescription, path) ⇒ Object
File writing
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 555 def writeFiles(models, projectName, projectDescription, path) outputFolder = path + "#{projectName}_PhariDoc/" FileUtils.mkdir_p(outputFolder) # Create the css/master.css file, which is default for any project FileUtils.mkdir_p(outputFolder + 'css/') css = File.open(outputFolder + 'css/master.css', 'w') writeCSS(css) css.close # Write the project main page projectHTML = File.open(outputFolder + 'project.html', 'w') writeProjectPage(projectName, projectHTML, models, projectDescription) projectHTML.close # Write each model's page FileUtils.mkdir_p(outputFolder + 'models/') models.each do |model| name = model.name.downcase modelHTML = File.open(outputFolder + "models/#{name}.html", 'w') writeModelPage(modelHTML, model, projectName) modelHTML.close end end |
#writeModelPage(file, model, project) ⇒ Object
Write a model HTML page content
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 601 def writeModelPage(file, model, project) variables = { name: model.name, relations: model.relations, methods: model.methods, routes: model.routes, model: model, project: project } erb_file = gem_libdir+'/templates/model.html.erb' erb_str = File.read(erb_file) # renderer = ERB.new(erb_str) renderer = Erubis::Eruby.new(erb_str) # result = renderer.result_with_hash(variables) result = renderer.result(variables) file.write(result) end |
#writeProjectPage(project, file, models, description) ⇒ Object
Write the main project HTML content
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 |
# File 'lib/phari_doc_gen/fileHandler.rb', line 584 def writeProjectPage(project, file, models, description) variables = { project: project, file: file, models: models, description: description } erb_file = gem_libdir+'/templates/project.html.erb' erb_str = File.read(erb_file) # renderer = ERB.new(erb_str) renderer = Erubis::Eruby.new(erb_str) # result = renderer.result_with_hash(variables) result = renderer.result(variables) file.write(result) end |