class FileHandler

Public Instance Methods

dataValue(argument) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 312
def dataValue(argument)
    if argument.include? "\s"
        name = argument.slice(0, argument.index("\s"))
        type = argument.slice(argument.index("\s") + 1, argument.size - 1)
        data = MethodParam.new(name, type)
        # data.printSelf
        return data
    end
    'nil'
end
decodeArgument(line) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 300
def decodeArgument(line)
    args = []
    initialIndex = line.index('@') + 1
    finalIndex = line.index("\s", line.index('@'))
    lenght = finalIndex - initialIndex
    keyword = line.slice(initialIndex, lenght)
    argument = line.slice(finalIndex + 1, line.size - finalIndex)
    args << keyword
    args << argument
    args
end
decodeDescription(line) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 323
def decodeDescription(line)
    initialIndex = line.index('*') + 3
    lenght = line.size - initialIndex
    description = line.slice(initialIndex, lenght)
    # puts "#{description}"
    description
end
hasBetween?(line, stringA, stringB) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 102
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) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 288
def hasDescription?(line)
    if (line.include? '#') && (line.include? '**')
        if line.index('#') < line.index('**')
            true
        else
            false
        end
    else
        false
    end
end
hasKeyword?(line) click to toggle source

Métodos de decodificação da leitura

# File lib/PhariDocGen/FileHandler.rb, line 276
def hasKeyword?(line)
    if (line.include? '#') && (line.include? '@')
        if line.index('#') < line.index('@')
            true
        else
            false
        end
    else
        false
    end
end
higlightText(line) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 112
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
    # puts line
    line
end
italicText(line) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 123
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
    # puts line
    line
end
linkText(line) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 134
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
packageExistis?(packageName) click to toggle source

Leitura dos arquivos

# File lib/PhariDocGen/FileHandler.rb, line 9
def packageExistis?(packageName)
    found = false
    package = ''
    Dir.glob('/**/' + packageName + '/') do |folder|
        found = true
        package = folder
        # puts 'done'
    end
    packagePath = package.to_s
    # puts packagePath
    if found
        return packagePath
    else
        puts 'Aborting: package not found'
        exit
    end
end
readFiles(project) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 35
def readFiles(project)
    models = []
    path = packageExistis?(project)
    Dir.glob(path + 'backend/api/models/*.rb') do |file|
        filename = file.to_s
        inputFile = File.new(filename)
        modelname = File.basename(inputFile, '.rb')
        helpername = path + 'backend/api/app/helpers/' + modelname + '_helper.rb'
        controllername = path + 'backend/api/app/controllers/' + modelname + '.rb'
        if File.exist?(helpername) && File.exist?(controllername)
            modelHelper = File.new(helpername, 'r')
            modelController = File.new(controllername, 'r')
            methods = readMethods(modelHelper)
            routes = readRoutes(modelController)
            currentModel = Modelo.new(modelname, methods, routes)
            models << currentModel
        end
    end
    models
end
readMethods(helper) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 149
def readMethods(helper)
    tipo = ''
    nome = ''
    descricao = ''
    inputs = []
    outputs = []
    methods = []
    toSent = true
    arr = IO.readlines(helper)
    arr.each do |line|
        if hasKeyword?(line)
            arg = decodeArgument(line)
            keyword = arg[0]
            argument = arg[1]
            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
            # methods.last.printName
            # puts "#{methods.size}"
            when 'param'
                data = dataValue(argument)
                inputs << data
            when 'return'
                data = dataValue(argument)
                outputs << data
            end
        end
        descricao = decodeDescription(line) if hasDescription?(line)
    end
    methods << Metodo.new(nome, tipo, inputs, outputs, descricao)
    methods
end
readProject(project) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 27
def readProject(project)
    path = packageExistis?(project)
    projectDescription = ''
    readme = File.new(path + 'README.md')
    projectDescription = readProjectDescription(readme)
    projectDescription
end
readProjectDescription(readme) click to toggle source

Métodos auxiliares a leitura

# File lib/PhariDocGen/FileHandler.rb, line 57
def readProjectDescription(readme)
    isCode = false
    @projectDescription = ''
    arr = IO.readlines(readme)
    arr.each do |line|
        next if line.start_with?('# ')
        if line.start_with?('## ')
            line = '<h3>' + line.slice(3, line.length - 3)
            line += '</h3>'
            # puts line
        end
        if line.start_with?('### ')
            line = '<h4>' + line.slice(4, line.length - 4) + '</h4>'
            # puts line
        end
        if line.start_with?('##### ')
            line = '<h5>' + line.slice(6, line.length - 6) + '</h5>'
            # puts line
        end
        if line.start_with?('* ')
            line = '<li>' + line.slice(2, line.length - 2) + '</li>'
            # puts line
        end
        while hasBetween?(line, '**', '**')
            line = higlightText(line)
            # puts line
        end
        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
    @projectDescription
end
readRoutes(controller) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 197
def readRoutes(controller)
    tipo = ''
    requisicao = ''
    dataInput = ''
    nome = ''
    inputs = []
    outputs = []
    routes = []
    toSent = true
    arr = IO.readlines(controller)
    arr.each do |line|
        next unless hasKeyword?(line)
        arg = decodeArgument(line)
        keyword = arg[0]
        argument = arg[1]
        case keyword
        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 << data
        when 'return'
            data = dataValue(argument)
            outputs << data
        end
    end
    routes << Rota.new(nome, tipo, requisicao, dataInput, inputs, outputs)
    routes
end
writeCSS(file) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 350
  def writeCSS(file)
      file.write("body{
  background-color: #702794;
}
a{
  color: #333;
}
li{
  padding-left: 3%;
}
.logo{
  margin: 5%;
}
.left{
  float: left;
}
.right{
  float: right;
}
.list-indexes{
  font-size: 24px;
}")
  end
writeFiles(models, project, projectDescription, path) click to toggle source

Escrita dos arquivos

# File lib/PhariDocGen/FileHandler.rb, line 332
def writeFiles(models, project, projectDescription, path)
    FileUtils.mkdir_p(path + 'css')
    css = File.open(path + 'css/master.css', 'w')
    writeCSS(css)
    css.close
    projectHTML = File.open(path + 'project.html', 'w')
    writeHeader(projectHTML)
    writeProjectPage(project, projectHTML, models, projectDescription)
    projectHTML.close
    FileUtils.mkdir_p(path + 'models')
    models.each do |model|
        name = model.name.downcase
        modelHTML = File.open(path + "models/#{name}.html", 'w')
        writeHeaderInDir(modelHTML)
        writeModelPage(modelHTML, model)
    end
end
writeHeader(file) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 374
def writeHeader(file)
    file.write("<!DOCTYPE html>

    <head lang='en'>
        <meta charset='UTF-8'>
        <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' integrity='sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u' crossorigin='anonymous'>
        <script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js' integrity='sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa' crossorigin='anonymous'></script>
        <link rel='stylesheet' href='css/master.css'>
    </head>

    <body>
        <nav class='navbar navbar-default navbar-fixed-top'>
            <div class='container-fluid'>
                <div class='navbar-header'>
                  <a href='project.html'><img class='logo' src='https://lh6.googleusercontent.com/pjGCZHp28gHcczrlPXsXq91al6hGwOkI5r84860enhunQJGbAsmX1w-eXr2_X6M_5bxgajlNgQz6Sy8=w1600-h810' height='50px' alt='Logo'></a>
                </div>
                <ul class='nav navbar-nav'>
                </ul>
            </div>
        </nav>
        <br><br><br>")
end
writeHeaderInDir(file) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 397
def writeHeaderInDir(file)
    file.write("<!DOCTYPE html>

    <head lang='en'>
        <meta charset='UTF-8'>
        <link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' integrity='sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u' crossorigin='anonymous'>
        <script src='https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js' integrity='sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa' crossorigin='anonymous'></script>
        <link rel='stylesheet' href='../css/master.css'>
    </head>

    <body>
        <nav class='navbar navbar-default navbar-fixed-top'>
            <div class='container-fluid'>
                <div class='navbar-header'>
                  <a href='../project.html'><img class='logo' src='https://lh6.googleusercontent.com/pjGCZHp28gHcczrlPXsXq91al6hGwOkI5r84860enhunQJGbAsmX1w-eXr2_X6M_5bxgajlNgQz6Sy8=w1600-h810' height='50px' alt='Logo'></a>
                </div>
                <ul class='nav navbar-nav'>
                </ul>
            </div>
        </nav>
        <br><br><br>")
end
writeModelPage(file, model) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 452
  def writeModelPage(file, model)
      name = model.name.capitalize
      file.write("<div class='container panel panel-default'>
        <div class='row'>
          <div class='panel panel-default'>
            <div class='panel-heading'>
              <h1>PhariSchool</h1>
            </div>
            <div class='panel-body'>
              <h2>#{name} Model</h2>
            </div>
          </div>
        </div>
        <div class='row'>
          <div class='col-md-6'>
            <div class='panel panel-default'>
              <div class='panel-heading'>
                <h2>Methods</h2>
              </div>
            </div>
            <ul class='list-group'>")
      methods = model.methods
      methods.each do |method|
          type = method.type
          name = method.name
          inputs = method.inputs
          outputs = method.outputs
          description = method.description
          file.write("            <div class='list-group-item'>
                        <h4 class='list-group-item-heading'>#{type} - #{name}</h4>
                        <p><br>Input:<br>
                        <ul>")
          inputs.each do |input|
              type = input.type
              name = input.name
              file.write("<li>#{type}: #{name}</li>")
          end
          file.write("</ul><br>Output:<br>
                            <ul>")
          outputs.each do |output|
              if output != 'nil'
                  type = output.type
                  name = output.name
                  file.write("<li>#{type}: #{name}</li>")
              else
                  file.write('<li>Null</li>')
              end
          end
          file.write("</ul><br>Description: #{description}</p>
                      </div>")
      end
      file.write("</ul>
    </div>
    <div class='col-md-6'>
      <div class='panel panel-default'>
        <div class='panel-heading'>
            <h2>Routes</h2>
        </div>
      </div>
      <ul class='list-group'>")
      routes = model.routes
      routes.each do |route|
          type = route.type
          name = route.name
          request = route.request
          dataInput = route.dataInput
          inputs = route.inputs
          outputs = route.outputs
          file.write("            <div class='list-group-item'>
                        <h4 class='list-group-item-heading'>Route type #{type} - #{name}</h4>
                        <p><br>Request: #{request}<br>
                        <br>Notation type: #{dataInput}<br>
                        <br>Input:<br>
                        <ul>")
          inputs.each do |input|
              type = input.type
              name = input.name
              file.write("<li>#{type}: #{name}</li>")
          end
          file.write("</ul><br>Output:<br>
                            <ul>")
          outputs.each do |output|
              if output != 'nil'
                  type = output.type
                  name = output.name
                  file.write("<li>#{type}: #{name}</li>")
              else
                  file.write('<li>Null</li>')
              end
          end
          file.write("</ul></p>
                      </div>")
      end
      file.write("</ul>
    </div>
  </div>
</div>
</body>")
  end
writeProjectPage(project, file, models, description) click to toggle source
# File lib/PhariDocGen/FileHandler.rb, line 420
def writeProjectPage(project, file, models, description)
    file.write("<div class='container panel panel-default'>
      <div class='row'>
        <div class='panel panel-default'>
          <div class='panel-heading'>
            <h1>#{project}</h1>
          </div>
          <div class='panel-body'>
            <h2>Project Description</h2>
            #{description}
          </div>
        </div>
      </div>
      <div class='row'>
        <div class='col-md-4'>
          <ul class='list-group'>")
    models.each do |model|
        link = model.name.downcase
        name = model.name.capitalize
        file.write("\t\t<a href='models/#{link}.html' class='list-indexes list-group-item'>#{name}</a>
        ")
    end
    file.write("\t</ul>
      </div>
      <div class='col-md-4 col-md-offset-2'>
        <img src='https://lh6.googleusercontent.com/pjGCZHp28gHcczrlPXsXq91al6hGwOkI5r84860enhunQJGbAsmX1w-eXr2_X6M_5bxgajlNgQz6Sy8=w1600-h810' alt='Phari logo' width='100%'>
      </div>
    </div>
  </div>
</body>")
end