Render source page layout with Askama · rust-lang/rust@102c8fa (original) (raw)
`@@ -65,23 +65,6 @@ pub(crate) fn render_item_decl_with_highlighting(src: &str, out: &mut Buffer) {
`
65
65
`write!(out, "");
`
66
66
`}
`
67
67
``
68
``
`` -
/// Highlights src
as a source code page, returning the HTML output.
``
69
``
`-
pub(crate) fn render_source_with_highlighting(
`
70
``
`-
src: &str,
`
71
``
`-
out: &mut Buffer,
`
72
``
`-
line_numbers: Buffer,
`
73
``
`-
href_context: HrefContext<'_, '_>,
`
74
``
`-
decoration_info: DecorationInfo,
`
75
``
`-
extra: Option<&str>,
`
76
``
`-
) {
`
77
``
`-
write_header(out, "", Some(line_numbers), Tooltip::None);
`
78
``
`-
if let Some(extra) = extra {
`
79
``
`-
out.push_str(extra);
`
80
``
`-
}
`
81
``
`-
write_code(out, src, Some(href_context), Some(decoration_info));
`
82
``
`-
write_footer(out, None);
`
83
``
`-
}
`
84
``
-
85
68
`fn write_header(out: &mut Buffer, class: &str, extra_content: Option, tooltip: Tooltip) {
`
86
69
`write!(
`
87
70
` out,
`
`@@ -143,8 +126,8 @@ fn can_merge(class1: Option, class2: Option, text: &str) -> bool {
`
143
126
``
144
127
`/// This type is used as a conveniency to prevent having to pass all its fields as arguments into
`
145
128
`/// the various functions (which became its methods).
`
146
``
`-
struct TokenHandler<'a, 'tcx> {
`
147
``
`-
out: &'a mut Buffer,
`
``
129
`+
struct TokenHandler<'a, 'tcx, F: Write> {
`
``
130
`+
out: &'a mut F,
`
148
131
`` /// It contains the closing tag and the associated Class
.
``
149
132
`closing_tags: Vec<(&'static str, Class)>,
`
150
133
`` /// This is used because we don't automatically generate the closing tag on ExitSpan
in
``
`@@ -159,7 +142,7 @@ struct TokenHandler<'a, 'tcx> {
`
159
142
`href_context: Option<HrefContext<'a, 'tcx>>,
`
160
143
`}
`
161
144
``
162
``
`-
impl<'a, 'tcx> TokenHandler<'a, 'tcx> {
`
``
145
`+
impl<'a, 'tcx, F: Write> TokenHandler<'a, 'tcx, F> {
`
163
146
`fn handle_exit_span(&mut self) {
`
164
147
`` // We can't get the last closing_tags
element using pop()
because closing_tags
is
``
165
148
`` // being used in write_pending_elems
.
``
`@@ -211,7 +194,7 @@ impl<'a, 'tcx> TokenHandler<'a, 'tcx> {
`
211
194
`}
`
212
195
`}
`
213
196
``
214
``
`-
impl<'a, 'tcx> Drop for TokenHandler<'a, 'tcx> {
`
``
197
`+
impl<'a, 'tcx, F: Write> Drop for TokenHandler<'a, 'tcx, F> {
`
215
198
`/// When leaving, we need to flush all pending data to not have missing content.
`
216
199
`fn drop(&mut self) {
`
217
200
`if self.pending_exit_span.is_some() {
`
`@@ -233,8 +216,8 @@ impl<'a, 'tcx> Drop for TokenHandler<'a, 'tcx> {
`
233
216
`/// item definition.
`
234
217
`///
`
235
218
`/// More explanations about spans and how we use them here are provided in the
`
236
``
`-
fn write_code(
`
237
``
`-
out: &mut Buffer,
`
``
219
`+
pub(super) fn write_code(
`
``
220
`+
out: &mut impl Write,
`
238
221
`src: &str,
`
239
222
`href_context: Option<HrefContext<'_, '_>>,
`
240
223
`decoration_info: Option,
`
`@@ -883,7 +866,7 @@ impl<'src> Classifier<'src> {
`
883
866
`/// Called when we start processing a span of text that should be highlighted.
`
884
867
`` /// The Class
argument specifies how it should be highlighted.
``
885
868
`fn enter_span(
`
886
``
`-
out: &mut Buffer,
`
``
869
`+
out: &mut impl Write,
`
887
870
`klass: Class,
`
888
871
`href_context: &Option<HrefContext<'_, '_>>,
`
889
872
`) -> &'static str {
`
`@@ -894,8 +877,8 @@ fn enter_span(
`
894
877
`}
`
895
878
``
896
879
`/// Called at the end of a span of highlighted text.
`
897
``
`-
fn exit_span(out: &mut Buffer, closing_tag: &str) {
`
898
``
`-
out.write_str(closing_tag);
`
``
880
`+
fn exit_span(out: &mut impl Write, closing_tag: &str) {
`
``
881
`+
out.write_str(closing_tag).unwrap();
`
899
882
`}
`
900
883
``
901
884
`/// Called for a span of text. If the text should be highlighted differently
`
`@@ -915,15 +898,15 @@ fn exit_span(out: &mut Buffer, closing_tag: &str) {
`
915
898
`` /// will then try to find this span
in the span_correspondance_map
. If found, it'll then
``
916
899
`/// generate a link for this element (which corresponds to where its definition is located).
`
917
900
`fn string<T: Display>(
`
918
``
`-
out: &mut Buffer,
`
``
901
`+
out: &mut impl Write,
`
919
902
`text: T,
`
920
903
`klass: Option,
`
921
904
`href_context: &Option<HrefContext<'_, '_>>,
`
922
905
`open_tag: bool,
`
923
906
`) {
`
924
907
`if let Some(closing_tag) = string_without_closing_tag(out, text, klass, href_context, open_tag)
`
925
908
`{
`
926
``
`-
out.write_str(closing_tag);
`
``
909
`+
out.write_str(closing_tag).unwrap();
`
927
910
`}
`
928
911
`}
`
929
912
``
`@@ -937,24 +920,24 @@ fn string<T: Display>(
`
937
920
`` /// in span_map.rs::collect_spans_and_sources
. If it cannot retrieve the information, then it's
``
938
921
`` /// the same as the second point (klass
is Some
but doesn't have a [rustc_span::Span
]).
``
939
922
`fn string_without_closing_tag<T: Display>(
`
940
``
`-
out: &mut Buffer,
`
``
923
`+
out: &mut impl Write,
`
941
924
`text: T,
`
942
925
`klass: Option,
`
943
926
`href_context: &Option<HrefContext<'_, '_>>,
`
944
927
`open_tag: bool,
`
945
928
`) -> Option<&'static str> {
`
946
929
`let Some(klass) = klass
`
947
930
`else {
`
948
``
`-
write!(out, "{}", text);
`
``
931
`+
write!(out, "{}", text).unwrap();
`
949
932
`return None;
`
950
933
`};
`
951
934
`let Some(def_span) = klass.get_span()
`
952
935
`else {
`
953
936
`if !open_tag {
`
954
``
`-
write!(out, "{}", text);
`
``
937
`+
write!(out, "{}", text).unwrap();
`
955
938
`return None;
`
956
939
`}
`
957
``
`-
write!(out, "<span class="{}">{}", klass.as_html(), text);
`
``
940
`+
write!(out, "<span class="{}">{}", klass.as_html(), text).unwrap();
`
958
941
`return Some("");
`
959
942
`};
`
960
943
``
`@@ -1009,28 +992,28 @@ fn string_without_closing_tag<T: Display>(
`
1009
992
`if !open_tag {
`
1010
993
`// We're already inside an element which has the same klass, no need to give it
`
1011
994
`// again.
`
1012
``
`-
write!(out, "<a href="{}">{}", href, text_s);
`
``
995
`+
write!(out, "<a href="{}">{}", href, text_s).unwrap();
`
1013
996
`} else {
`
1014
997
`let klass_s = klass.as_html();
`
1015
998
`if klass_s.is_empty() {
`
1016
``
`-
write!(out, "<a href="{}">{}", href, text_s);
`
``
999
`+
write!(out, "<a href="{}">{}", href, text_s).unwrap();
`
1017
1000
`} else {
`
1018
``
`-
write!(out, "<a class="{}" href="{}">{}", klass_s, href, text_s);
`
``
1001
`+
write!(out, "<a class="{}" href="{}">{}", klass_s, href, text_s).unwrap();
`
1019
1002
`}
`
1020
1003
`}
`
1021
1004
`return Some("");
`
1022
1005
`}
`
1023
1006
`}
`
1024
1007
`if !open_tag {
`
1025
``
`-
write!(out, "{}", text_s);
`
``
1008
`+
write!(out, "{}", text_s).unwrap();
`
1026
1009
`return None;
`
1027
1010
`}
`
1028
1011
`let klass_s = klass.as_html();
`
1029
1012
`if klass_s.is_empty() {
`
1030
``
`-
write!(out, "{}", text_s);
`
``
1013
`+
out.write_str(&text_s).unwrap();
`
1031
1014
`Some("")
`
1032
1015
`} else {
`
1033
``
`-
write!(out, "<span class="{}">{}", klass_s, text_s);
`
``
1016
`+
write!(out, "<span class="{}">{}", klass_s, text_s).unwrap();
`
1034
1017
`Some("")
`
1035
1018
`}
`
1036
1019
`}
`