Skip to content

Commit

Permalink
Fix issue with Google Translate (#2266)
Browse files Browse the repository at this point in the history
* Fix issue where Google Translate processed code with tooltips
* always use pre+code tag with prettify to support translators

fixes #2260
  • Loading branch information
johnpryan authored Feb 10, 2020
1 parent db676a2 commit a720ac4
Show file tree
Hide file tree
Showing 52 changed files with 965 additions and 959 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ For example, say you want to change the following code in the [language tour](h

```
<?code-excerpt "misc/lib/language_tour/variables.dart (var-decl)"?>
{% prettify dart %}
{% prettify dart tag=pre+code %}
var name = 'Bob';
{% endprettify %}
```
Expand Down
44 changes: 22 additions & 22 deletions src/_articles/archive/converters-and-codecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ they had been used separately.
A codec is a combination of two converters where one encodes
and the other one decodes:

{% prettify dart %}
{% prettify dart tag=pre+code %}
abstract class Codec<S, T> {
const Codec();

Expand Down Expand Up @@ -76,7 +76,7 @@ arguments. For example, the
adds named arguments to `encode()` and `decode()`
to make these methods more useful:

{% prettify dart %}
{% prettify dart tag=pre+code %}
dynamic decode(String source, {reviver(var key, var value)}) { … }
String encode(Object value, {toEncodable(var object)}) { … }
{% endprettify %}
Expand All @@ -85,7 +85,7 @@ The codec can be instantiated with arguments that are used as default
values, unless they are overridden by the named arguments during the
`encode()`/`decode()` call.

{% prettify dart %}
{% prettify dart tag=pre+code %}
const JsonCodec({reviver(var key, var value), toEncodable(var object)})
...
{% endprettify %}
Expand All @@ -100,7 +100,7 @@ When possible, codec constructors should be `const` constructors.
Converters, and in particular their `convert()` methods, are
where the real conversions happen:

{% prettify dart %}
{% prettify dart tag=pre+code %}
T convert(S input); // where T is the target and S the source type.
{% endprettify %}

Expand All @@ -120,7 +120,7 @@ interface and can thus be given to the `Stream.transform()` method.
Probably the most common use case is the decoding of UTF-8 with
[utf8.decoder]({{site.dart_api}}/{{site.data.pkg-vers.SDK.channel}}/dart-convert/Utf8Codec-class.html):

{% prettify dart %}
{% prettify dart tag=pre+code %}
File.openRead().transform(utf8.decoder).
{% endprettify %}

Expand Down Expand Up @@ -181,7 +181,7 @@ has the same signature as
HtmlEscape: String→String. In this case each individual output chunk
represents one line.

{% prettify dart %}
{% prettify dart tag=pre+code %}
import 'dart:convert';
import 'dart:async';

Expand Down Expand Up @@ -238,7 +238,7 @@ accepts a list (which may not be held onto), the sub-range that the converter
operates on, and a boolean `isLast`, which can be set instead of calling
`close()`.

{% prettify dart %}
{% prettify dart tag=pre+code %}
import 'dart:convert';

main() {
Expand Down Expand Up @@ -281,7 +281,7 @@ converter and how a custom ChunkedConversionSink can improve performance.
Let’s start with the simple synchronous converter,
whose encryption routine simply rotates bytes by the given key:

{% prettify dart %}
{% prettify dart tag=pre+code %}
import 'dart:convert';

/// A simple extension of Rot13 to bytes and a key.
Expand All @@ -302,7 +302,7 @@ class RotConverter extends Converter<List<int>, List<int>> {

The corresponding Codec class is also simple:

{% prettify dart %}
{% prettify dart tag=pre+code %}
class Rot extends Codec<List<int>, List<int>> {
final _key;
const Rot(this._key);
Expand All @@ -327,7 +327,7 @@ allocate a new instance of RotConverter every time one is needed.

This is how we use the Rot codec:

{% prettify dart %}
{% prettify dart tag=pre+code %}
const Rot ROT128 = const Rot(128);
const Rot ROT1 = const Rot(1);
main() {
Expand All @@ -345,7 +345,7 @@ We are on the right track. The codec works, but it is still missing the chunked
encoding part. Because each byte is encoded separately we can fall back to
the synchronous conversion method:

{% prettify dart %}
{% prettify dart tag=pre+code %}
class RotConverter {
...
RotSink startChunkedConversion(sink) {
Expand All @@ -372,7 +372,7 @@ class RotSink extends ChunkedConversionSink<List<int>> {
Now, we can use the converter with chunked conversions or even for stream
transformations:

{% prettify dart %}
{% prettify dart tag=pre+code %}
// Requires to import dart:io.
main(args) {
String inFile = args[0];
Expand Down Expand Up @@ -406,7 +406,7 @@ We can also avoid the allocation altogether if we overwrite the input. In
the following version of RotSink, we add a new method `addModifiable()` that
does exactly that:

{% prettify dart %}
{% prettify dart tag=pre+code %}
class RotSink extends ChunkedConversionSink<List<int>> {
final _key;
final ChunkedConversionSink<List<int>> _outSink;
Expand Down Expand Up @@ -436,7 +436,7 @@ advanced method (for example `addModifiableSlice()`) would take range arguments
This new method is not yet used by transformers, but we can already use it when
invoking `startChunkedConversion()` explicitly.

{% prettify dart %}
{% prettify dart tag=pre+code %}

main() {
var outSink = new ChunkedConversionSink.withCallback((chunks) {
Expand Down Expand Up @@ -465,7 +465,7 @@ such a converter would be the potential source of hard-to-track bugs. Instead,
we write a converter that does the unmodifiable-to-modifiable conversion
explicitly, and then fuse the two converters.

{% prettify dart %}
{% prettify dart tag=pre+code %}
class ToModifiableConverter extends Converter<List<int>, List<int>> {
List<int> convert(List<int> data) => data;
ToModifiableSink startChunkedConversion(RotSink sink) {
Expand All @@ -486,7 +486,7 @@ class ToModifiableSink
ToModifiableSink just signals the next sink that the incoming chunk
is modifiable. We can use this to make our pipeline more efficient:

{% prettify dart %}
{% prettify dart tag=pre+code %}
main(args) {
String inFile = args[0];
String outFile = args[1];
Expand All @@ -509,7 +509,7 @@ converters and not just with our Rot cipher. We should therefore make an
interface that generalizes the concept. For simplicity, we named it
`CipherSink`, although it has, of course, uses outside the encryption world.

{% prettify dart %}
{% prettify dart tag=pre+code %}
abstract class CipherSink
extends ChunkedConversionSink<List<int>, List<int>> {
void addModifiable(List<int> data) { add(data); }
Expand All @@ -526,7 +526,7 @@ Although we won’t make the cipher faster anymore,
we can improve the output side of our Rot converter.
Take, for instance, the fusion of two encryptions:

{% prettify dart %}
{% prettify dart tag=pre+code %}
main(args) {
String inFile = args[0];
String outFile = args[1];
Expand All @@ -546,7 +546,7 @@ Since the first RotConverter invokes `outSink.add()`, the second RotConverter
assumes that input cannot be modified and allocates a copy. We can work around
this by sandwiching a ToModifiableConverter in between the two ciphers:

{% prettify dart %}
{% prettify dart tag=pre+code %}
var transformer = new ToModifiableConverter()
.fuse(new RotConverter(key))
.fuse(new ToModifiableConverter())
Expand All @@ -560,7 +560,7 @@ whenever we want to add a new chunk,
or at the beginning when we start a chunked
conversion. We prefer the latter approach:

{% prettify dart %}
{% prettify dart tag=pre+code %}
/// Works more efficiently if given a CipherSink as argument.
CipherSink startChunkedConversion(
ChunkedConversionSink<List<int>> sink) {
Expand All @@ -571,7 +571,7 @@ conversion. We prefer the latter approach:

_CipherSinkAdapter is simply:

{% prettify dart %}
{% prettify dart tag=pre+code %}
class _CipherSinkAdapter implements CipherSink {
ChunkedConversionSink<List<int>, List<int>> sink;
_CipherSinkAdapter(this.sink);
Expand All @@ -585,7 +585,7 @@ class _CipherSinkAdapter implements CipherSink {
We now only need to change the _RotSink to take advantage of the fact that it
always receives a CipherSink as an argument to its constructor:

{% prettify dart %}
{% prettify dart tag=pre+code %}
class _RotSink extends CipherSink {
final _key;
final CipherSink _outSink; // <= always a CipherSink.
Expand Down
32 changes: 16 additions & 16 deletions src/_articles/archive/zones.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ For example,
a simple HTTP server
might use the following code:

{% prettify dart %}
{% prettify dart tag=pre+code %}
[!runZoned(() {!]
HttpServer.bind('0.0.0.0', port).then((server) {
server.listen(staticFiles.serveRequest);
Expand Down Expand Up @@ -158,7 +158,7 @@ that's invoked for every uncaught error in the zone.
For example:

<!-- run_zoned1.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
runZoned(() {
Timer.run(() { throw 'Would normally kill the program'; });
}, onError: (error, stackTrace) {
Expand Down Expand Up @@ -203,7 +203,7 @@ the error raised by the first line
can't cross into an error zone.

<!-- run_zoned2.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
var f = new Future.error(499);
f = f.whenComplete(() { print('Outside runZoned'); });
runZoned(() {
Expand All @@ -216,7 +216,7 @@ runZoned(() {

Here's the output you see if you run the example:

{% prettify xml %}
{% prettify xml tag=pre+code %}
Outside runZoned
Inside non-error zone
Uncaught Error: 499
Expand All @@ -229,7 +229,7 @@ If you remove the calls to `runZoned()` or
remove the `onError` argument,
you see this output:

{% prettify xml %}
{% prettify xml tag=pre+code %}
Outside runZoned
Inside non-error zone
[!Inside error zone (not called)!]
Expand All @@ -255,7 +255,7 @@ Similarly, errors can't cross _out_ of error zones.
Consider this example:

<!-- run_zoned3.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
var completer = new Completer();
var future = completer.future.then((x) => x + 1);
var zoneFuture;
Expand Down Expand Up @@ -296,7 +296,7 @@ which aren't evaluated until you ask for values.
Here is an example of using a stream with `runZoned()`:

<!-- stream.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
var stream = new File('stream.dart').openRead()
.map((x) => throw 'Callback throws');

Expand All @@ -308,7 +308,7 @@ The exception thrown by the callback
is caught by the error handler of `runZoned()`.
Here's the output:

{% prettify xml %}
{% prettify xml tag=pre+code %}
Caught error: Callback throws
{% endprettify %}

Expand All @@ -332,7 +332,7 @@ Use the `zoneValues` argument to `runZoned()` to
store values in the newly created zone:

<!-- value1.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
runZoned(() {
print(Zone.current[#key]);
}, zoneValues: { #key: 499 });
Expand All @@ -351,7 +351,7 @@ For example, the following code
adds an item to a zone-local list:

<!-- value1_1.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
runZoned(() {
Zone.current[#key].add(499);
print(Zone.current[#key]); // [499]
Expand All @@ -376,7 +376,7 @@ and want to print all of their lines.
The program might look like this:

<!-- value2.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
import 'dart:async';
import 'dart:convert';
import 'dart:io';
Expand Down Expand Up @@ -406,7 +406,7 @@ With zone-local values you can add the filename to the returned string
(new lines are highlighted):

<!-- value3.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
import 'dart:async';
import 'dart:convert';
import 'dart:io';
Expand Down Expand Up @@ -460,7 +460,7 @@ As a simple example of overriding functionality,
here is a way to silence all prints inside a zone:

<!-- specification1.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
import 'dart:async';

main() {
Expand Down Expand Up @@ -535,7 +535,7 @@ for another interceptable method, `scheduleMicrotask()`:
Here is an example that shows how to delegate to the parent zone:

<!-- specification2.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
import 'dart:async';

main() {
Expand Down Expand Up @@ -585,7 +585,7 @@ that executes just after calling `runZoned()`.
Here's an example of profiling code using `run*`:

<!-- profile_run.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
final total = new Stopwatch();
final user = new Stopwatch();

Expand Down Expand Up @@ -640,7 +640,7 @@ save a stack trace
before the code disappears into an asynchronous context.

<!-- debug.dart -->
{% prettify dart %}
{% prettify dart tag=pre+code %}
import 'dart:async';

get currentStackTrace {
Expand Down
Loading

0 comments on commit a720ac4

Please sign in to comment.