1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-08 13:58:47 +00:00
Files
samples/web/samples_index/lib/cookbook.dart
John Ryan 0a5a5109de Add samples index (#359)
* add samples_index to web/ directory

Co-authored-by: Thea Flowers <theaflowers@google.com>

* add pub_get.dart script

* build sample index in peanut post build

* re-generate sample index with web demos

* print more details in peanut_post_build.dart

* add images for demos

* run generator

* update README

* add animations and provider shopper as symlinks

* add links to symlinked web demos

* use relative paths

* update cookbook images, urls, and description CSS

* use relative URL for navbar link

* unstage HTML files

* .gitignore generated HTML files

* add margin to toolbar

* rename escape functions

* add and update copyright headers

Co-authored-by: Thea Flowers <theaflowers@google.com>
2020-03-09 16:17:08 -07:00

77 lines
2.3 KiB
Dart

// Copyright 2020 The Flutter team. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file
import 'dart:io';
import 'package:samples_index/src/data.dart';
/// Utilities for generating cookbook article data
import 'package:webdriver/io.dart';
import 'package:html/parser.dart' show parse;
import 'package:path/path.dart' as path;
class CookbookScraper {
WebDriver _driver;
Future init() async {
_driver = await createDriver(desired: {});
}
Future dispose() async {
await _driver.quit();
}
Future<List<String>> fetchCookbookLinks() async {
var flutterUrl = 'https://flutter.dev';
var url = Uri.parse('$flutterUrl/docs/cookbook');
await _driver.get(url);
var pageContent = await _driver.pageSource;
var page = parse(pageContent);
var links = page.querySelectorAll('main>.container>ul>li>a');
return links.map((e) => '$flutterUrl${e.attributes["href"]}').toList();
}
Future<Sample> getMetadata(String url) async {
await _driver.get(Uri.parse(url));
var pageContent = await _driver.pageSource;
var page = parse(pageContent);
var name = page.querySelector('main>.container>header>h1').text;
var description = page.querySelectorAll('main>.container>p').first.text;
var urlSegments = Uri.parse(url).pathSegments;
var category = urlSegments[urlSegments.length-2];
return Sample(
name: name,
description: description,
author: 'Flutter',
type: 'cookbook',
screenshots: [Screenshot(screenshotPath(url), 'Cookbook article')],
tags: ['cookbook', category],
source: url,
);
}
Future takeScreenshot(String url) async {
var screenshot = await _driver.captureScreenshotAsList();
var file = File('web${screenshotPath(url)}');
await file.create(recursive: true);
await file.writeAsBytes(screenshot);
}
}
String screenshotPath(String url) {
var filename = parseFileName(url);
return 'images/cookbook/$filename.png';
}
/// Parses a filename from a cookbook link. E.g.
/// `https://flutter.dev/docs/cookbook/navigation/returning-data.html` changes
/// to `returning_data.png`
String parseFileName(String link) {
var p = path.basename(link);
var dot = p.indexOf('.');
return p.substring(0, dot);
}