<commit_before>module SynvertToolsApp
  module Routes
    class Root < Base
      configure do
        set :views, 'app/views/root'
      end

      get '/' do
        slim :index
      end

      post '/play' do
        code = params[:code].presence
        snippet = params[:snippet].presence

        result = SynvertTools.convert_code(code, snippet)

        if result
          result = result.gsub(/\n/, '<br>')
          result = result.gsub(/( )/, '&nbsp;')
        end

        content_type :json
        { result: result }.to_json
      end

      post '/convert' do
        code = params[:code].presence

        result = SynvertTools.to_ast_node(code).to_sexp

        if result
          result = result.gsub(/\n/, '<br>')
          result = result.gsub(/( )/, '&nbsp;')
        end

        content_type :json
        { result: result }.to_json
      end

      post '/match' do
        code = params[:code].presence
        rule = params[:rule].presence

        matchings = SynvertTools.matching_code(code, rule) if code && rule

        if matchings
          matchings[1] = matchings[1].gsub(/\n/, '<br>')
          matchings[1] = matchings[1].gsub(/(  )/, '&nbsp;&nbsp;')
        end

        content_type :json
        { matchings: matchings || [] }.to_json
      end
    end
  end
end<commit_msg>rescue Parser::SyntaxError<commit_after>module SynvertToolsApp
  module Routes
    class Root < Base
      configure do
        set :views, 'app/views/root'
      end

      get '/' do
        slim :index
      end

      post '/play' do
        code = params[:code].presence
        snippet = params[:snippet].presence

        begin
          result = SynvertTools.convert_code(code, snippet)

          if result
            result = result.gsub(/\n/, '<br>')
            result = result.gsub(/( )/, '&nbsp;')
          end

          content_type :json
          { result: result }.to_json
        rescue Parser::SyntaxError
          status 500
          {}.to_json
        end
      end

      post '/convert' do
        code = params[:code].presence

        begin
          result = SynvertTools.to_ast_node(code).to_sexp

          if result
            result = result.gsub(/\n/, '<br>')
            result = result.gsub(/( )/, '&nbsp;')
          end

          content_type :json
          { result: result }.to_json
        rescue Parser::SyntaxError
          status 500
          {}.to_json
        end
      end

      post '/match' do
        code = params[:code].presence
        rule = params[:rule].presence

        begin
          matchings = SynvertTools.matching_code(code, rule) if code && rule

          if matchings
            matchings[1] = matchings[1].gsub(/\n/, '<br>')
            matchings[1] = matchings[1].gsub(/(  )/, '&nbsp;&nbsp;')
          end

          content_type :json
          { matchings: matchings || [] }.to_json
        rescue Parser::SyntaxError
          status 500
          {}.to_json
        end
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.11841227484989994, 'fraction_numerical': 0.005670446964643095, 'mean_word_length': 1.580895008605852, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4694510', 'n_tokens_mistral': 923, 'n_tokens_neox': 892, 'n_words': 213}