💾 Archived View for clanmorgan.org › gemlog › 2023-04-25-dabbling-in-dart.gmi captured on 2023-04-26 at 12:55:52. Gemini links have been rewritten to link to archived content

View Raw

More Information

➡️ Next capture (2023-05-24)

-=-=-=-=-=-=-

Dabbling in Dart

I work at a big tech company on a programming language called Dart.

So, naturally I use it for any hobby programming and will probably end up writing Dart code to interact with Gemini.

Dart is a modern object-oriented language that’s easy to pick up; it looks like a simplified Java. Its big feature is that it runs everywhere: it compiles to javascript for the web, to native code for iOS and Android, and can run in JIT and native compiled modes on various desktop/server platforms.

You’re most likely to encounter Dart in day to day life as the language behind Flutter, the multi-platform app framework. But it’s just as well suited to simple scripting. Here is my first Dart code for Gemini:

import 'dart:io';

/// Replaces typewriter quotes and double dashes in all `.gmi` files under
/// the specified path with their nicer unicode equivalents.
///
/// Usage: dart fix_typography.dart <root path>
void main(List<String> arguments) {
  final gmis = Directory(arguments[0])
      .listSync(recursive: true)
      .whereType<File>()
      .where((f) => f.path.endsWith('.gmi'));
  for (final gmi in gmis) {
    print('Fixing ${gmi.path}.');
    final lines = gmi.readAsLinesSync();
    var skip = false;
    for (var i = 0; i != lines.length; ++i) {
      var line = lines[i];
      if (line.startsWith('```')) {
        skip = !skip;
        continue;
      }
      if (skip) continue;
      line = line.replaceAll("'", "’");
      line = line.replaceAll('--', '—');
      line = line.replaceAllMapped(RegExp(r'"(\w)'), (m) => '“${m.group(1)}');
      line = line.replaceAllMapped(RegExp(r'(\w)"'), (m) => '${m.group(1)}”');
      lines[i] = line;
    }
    gmi.writeAsStringSync(lines.join('\n'));
  }
}

It processes all `gmi` files under a root, replacing typewriter quotes and double dashes with their nicer unicode equivalents.

More fun that preprocessing would be a Gemini server in Dart; of course, if I write one, I’ll share!

Links

Dart: a client-optimized language for fast apps on any platform

Flutter: Build apps for any screen

📮

👍 Thanks!

👎 Not for me.

🤷 No opinion.

Comments.