<commit_before>library dartnow.dart_snippet;

import 'dart:html';
import 'package:dartnow/common.dart';
import 'package:github/browser.dart';
import 'dart:async';
import 'package:dartnow/user.dart';

class DartSnippet {
  final String name;
  final String description;
  final String mainLibrary;
  String mainElements;
  final String tags;
  final String id;
  final String author;
  final String dartpadUrl;
  final Map<String, String> dependencies;
  final String gistUrl;
  final List<String> libraries;
  final DateTime createdAt;
  final DateTime updatedAt;

  List<DartNowUser> users;
  DartNowUser user;

  DartSnippet.fromJSON(this.id, Map json, this.users)
      : name = json['name'],
        description = json['description'],
        mainLibrary = json['mainLibrary'],
        mainElements = json['mainElements'],
        dependencies = json['dependencies'],
        createdAt = json['createdAt'] == null
            ? null
            : DateTime.parse(json['createdAt']),
        updatedAt = json['updatedAt'] == null
            ? null
            : DateTime.parse(json['updatedAt']),
        tags = json['tags'],
        author = json['author'],
        dartpadUrl = json['dartpadUrl'],
        gistUrl = json['gistUrl'],
        libraries = json['libraries'] == null ? [] : json['libraries'] {
    libraries
      ..removeWhere((l) =>
    mainLibrary.contains(l))
      ..addAll(mainLibrary.split(' '))
      ..sort((String a, String b) {
        if (mainLibrary.contains(a)) {
          return -1;
        } else if (a.contains('dart')) {
          return 1;
        }
        return 1;
      });

    user = users.firstWhere((DartNowUser user) => user.username == author);
  }

  Future<DivElement> toHtml() async {
    List<String> temp = new List.from(libraries);

    temp..removeWhere((l) =>
    mainLibrary.contains(l));
    String libString = temp.isEmpty ? '' : '<code>${temp.join('</code> <code>')}</code>';
    mainElements = '<code>${mainElements.split(' ').join('</code> <code>')}</code>';
    print(libString);
    return new DivElement()
      ..setInnerHtml('''
      <div class="left">
    ${description}
    <b>Libraries:</b> <code><em>$mainLibrary</em></code> ${libString}<br>
    <b>Main element${mainElements.split(' ').length > 1 ? "s" : ""}:</b>
    ${mainElements}<br>
    <b>Gist:</b> <a href="${gistUrl}" target="_blank">${gistUrl}</a><br>
    ${libraries.every((l) => l.contains('dart:')) ?
    '''
      <b>Dartpad:</b>
      <a href="https://dartpad.dartlang.org/${id}" target="_blank">
        https://dartpad.dartlang.org/${id}
      </a><br>
    ''' : ''}
    ${(true || createdAt == null)  ? '' : '<b>Created </b> ${formatDate(createdAt)}<br>'}

    ${tags.length == 0 ? "" :<commit_msg>use short description for description<commit_after>library dartnow.dart_snippet;

import 'dart:html';
import 'package:dartnow/common.dart';
import 'package:github/browser.dart';
import 'dart:async';
import 'package:dartnow/user.dart';

class DartSnippet {
  final String name;
  final String description;
  final String shortDescription;
  final String mainLibrary;
  String mainElements;
  final String tags;
  final String id;
  final String author;
  final String dartpadUrl;
  final Map<String, String> dependencies;
  final String gistUrl;
  final List<String> libraries;
  final DateTime createdAt;
  final DateTime updatedAt;

  List<DartNowUser> users;
  DartNowUser user;

  DartSnippet.fromJSON(this.id, Map json, this.users)
      : name = json['name'],
        description = json['description'],
        shortDescription = json['shortDescription'],
        mainLibrary = json['mainLibrary'],
        mainElements = json['mainElements'],
        dependencies = json['dependencies'],
        createdAt = json['createdAt'] == null
            ? null
            : DateTime.parse(json['createdAt']),
        updatedAt = json['updatedAt'] == null
            ? null
            : DateTime.parse(json['updatedAt']),
        tags = json['tags'],
        author = json['author'],
        dartpadUrl = json['dartpadUrl'],
        gistUrl = json['gistUrl'],
        libraries = json['libraries'] == null ? [] : json['libraries'] {
    libraries
      ..removeWhere((l) =>
    mainLibrary.contains(l))
      ..addAll(mainLibrary.split(' '))
      ..sort((String a, String b) {
        if (mainLibrary.contains(a)) {
          return -1;
        } else if (a.contains('dart')) {
          return 1;
        }
        return 1;
      });

    user = users.firstWhere((DartNowUser user) => user.username == author);
  }

  Future<DivElement> toHtml() async {
    List<String> temp = new List.from(libraries);

    temp..removeWhere((l) =>
    mainLibrary.contains(l));
    String libString = temp.isEmpty ? '' : '<code>${temp.join('</code> <code>')}</code>';
    mainElements = '<code>${mainElements.split(' ').join('</code> <code>')}</code>';
    print(libString);
    return new DivElement()
      ..setInnerHtml('''
      <div class="left">
    ${shortDescription}
    <b>Libraries:</b> <code><em>$mainLibrary</em></code> ${libString}<br>
    <b>Main element${mainElements.split(' ').length > 1 ? "s" : ""}:</b>
    ${mainElements}<br>
    <b>Gist:</b> <a href="${gistUrl}" target="_blank">${gistUrl}</a><br>
    ${libraries.every((l) => l.contains('dart:')) ?
    '''
      <b>Dartpad:</b>
      <a href="https://dartpad.dartlang.org/${id}" target="_blank">
        https://dartpad.dartlang.org/${id}
      </a><br>
    ''' : ''}
    ${(true || createdAt == null)  ? '' : '<b>Created </b> ${formatDate(createdAt)}<br>'}

    ${tags.length == 0 ? "" :
{'fraction_non_alphanumeric': 0.17981749865807836, 'fraction_numerical': 0.001789228842368939, 'mean_word_length': 3.0536620739666427, 'pattern_counts': {'":': 0, '<': 77, '<?xml version=': 0, '>': 87, 'https://': 4, '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': '1739304', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1760, 'n_words': 355}