Port code from BBEncoder to MarkUp Encoder.