1
0
mirror of https://github.com/flutter/samples.git synced 2025-11-09 22:38:42 +00:00

Simplify samples index and remove cookbook recipes (#2102)

Remove the cookbook recipes from the samples index in a step to
eventually remove it as a whole.

The cookbook recipe listings in the index haven't been updated in a long
time, the support for updating them doesn't work, and this isn't
generally how people are finding cookbook recipes.

This has the added benefit of reducing repo size quite a bit due to the
large images.
This commit is contained in:
Parker Lougheed
2023-11-30 12:17:19 -06:00
committed by GitHub
parent 60a4057c1f
commit ab6d874404
120 changed files with 28 additions and 1563 deletions

View File

@@ -1,86 +0,0 @@
// 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:html/parser.dart' show parse;
import 'package:path/path.dart' as path;
import 'package:samples_index/src/data.dart';
/// Utilities for generating cookbook article data
import 'package:webdriver/async_io.dart';
class CookbookScraper {
late WebDriver _driver;
Future<void> init() async {
_driver = await createDriver(desired: <String, dynamic>{});
}
Future<void> 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 search = 'main>.container>header>h1';
var h1 = page.querySelector(search);
if (h1 == null) {
throw ('Could not find match for $search on page $url');
}
var name = 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,
difficulty: 'advanced',
);
}
Future<void> 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('.');
var detailName = p.substring(0, dot);
// var categoryName = path.split(link);
var components = path.split(link);
var categoryName = components[components.length - 2];
return '$categoryName-$detailName';
}

View File

@@ -2,7 +2,6 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file
import 'dart:convert';
import 'dart:io';
import 'package:checked_yaml/checked_yaml.dart';
@@ -12,14 +11,10 @@ export 'src/data.dart';
Future<List<Sample>> getSamples() async {
var yamlFile = File('lib/src/samples.yaml');
var cookbookFile = File('lib/src/cookbook.json');
var contents = await yamlFile.readAsString();
var cookbookContents = await cookbookFile.readAsString();
var index = checkedYamlDecode(
contents, (m) => m != null ? Index.fromJson(m) : null,
sourceUrl: yamlFile.uri);
if (index == null) throw ('unable to get load from ${yamlFile.uri}');
var cookbookIndex =
Index.fromJson(json.decode(cookbookContents) as Map<dynamic, dynamic>);
return index.samples..addAll(cookbookIndex.samples);
return index.samples;
}

File diff suppressed because it is too large Load Diff

View File

@@ -36,11 +36,10 @@ class Sample {
/// The author of the sample. Typically "Flutter"
final String? author;
/// Screenshots of the sample or cookbook article. At least 1 screenshot is
/// required.
/// Screenshots of the sample. At least 1 screenshot is required.
final List<Screenshot> screenshots;
/// A link to the source code or cookbook article if type is 'cookbook'.
/// A link to the source code.
final String source;
/// A link to this sample running in the browser.
@@ -69,7 +68,7 @@ class Sample {
final List<String> platforms;
/// The type of the sample. Supported values are either 'sample' or
/// 'cookbook'.
/// 'demo'.
final String type;
/// The date this sample was created.

View File

@@ -110,12 +110,6 @@ String _indexBody(List<Sample> samples) => '''
<span role="button" tabindex="-1" class="mdc-chip__text">Sample</span>
</span>
</div>
<div class="mdc-chip" role="row">
<div class="mdc-chip__ripple"></div>
<span role="gridcell">
<span role="button" tabindex="-1" class="mdc-chip__text">Cookbook</span>
</span>
</div>
<div class="mdc-chip" role="row">
<div class="mdc-chip__ripple"></div>
<span role="gridcell">
@@ -205,11 +199,6 @@ String _descriptionButtons(Sample sample) {
<span class="mdc-button__label">Source Code</span>
</button>''');
}
if (sample.type == 'cookbook') {
buf.write(
'''<button class="mdc-button mdc-button--outlined" onclick="window.location.href = '${sample.source}';"> <span class="mdc-button__ripple"></span>View Recipe</button>''');
}
return buf.toString();
}