germ_macros_impl/lib.rs
1// This file is part of Germ <https://github.com/gemrest/germ>.
2// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, version 3.
7//
8// This program is distributed in the hope that it will be useful, but
9// WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11// General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright (C) 2022-2022 Fuwn <contact@fuwn.me>
17// SPDX-License-Identifier: GPL-3.0-only
18
19#![deny(
20 warnings,
21 nonstandard_style,
22 unused,
23 future_incompatible,
24 rust_2018_idioms,
25 unsafe_code,
26 clippy::all,
27 clippy::nursery,
28 clippy::pedantic
29)]
30#![feature(proc_macro_hygiene, proc_macro_span)]
31#![recursion_limit = "128"]
32
33use proc_macro::TokenStream;
34
35/// Convert Gemtext into a token tree
36///
37/// # Panics
38///
39/// May panic if the Gemini could not be properly handled, for any reason.
40#[proc_macro]
41pub fn gemini_to_tt(input: TokenStream) -> TokenStream {
42 let mut tokens = input.into_iter();
43 let mut span = tokens.next().unwrap().span();
44
45 for token in tokens {
46 span = span.join(token.span()).unwrap();
47 }
48
49 let gemini = span
50 .source_text()
51 .unwrap()
52 .lines()
53 .map(|l| l.trim_start().to_string())
54 .collect::<Vec<String>>()
55 .join("\n");
56
57 quote::quote!(#gemini).into()
58}